View Sidebar
Implementing Custom Post Types with a Custom Plugin

Implementing Custom Post Types with a Custom Plugin

September 21, 2011 6:42 am1 comment

This post tells the story of my journey of learning about WordPress custom post types, playing with them, preparing a meetup presentation on them and implementing them on a client website. I ended up with a custom plugin file that contained all my code for registering, administering and using my custom post type on the site.

I first used the standard method of adding code to my theme’s functions.php file and creating a page template to display my post data. Justin Tadlock has a great tutorial for this. That method worked just fine for a while.

The problem with this method is that when/if I change themes, my custom post type code is gone because a new theme has its own functions.php file and page templates. I would have to edit the new theme to include my code and customizations.

I then came across an slide deck by Mark Jaquith where he mentioned using the Custom Post Type UI plugin and a custom plugin that creates a shortcode for displaying my custom data. This allows custom post types and display of data to be portable from theme to theme.

My goal with a client site was to create a way to list Graduates of the orthodontics department of the University of North Carolina’s school of dentistry (see www.uncoaa.com). I needed an easy way for a site editor or admin to enter new graduates and have them automatically appear correctly in the graduates list.

Creating the Custom Post Type

I installed the Custom Post Type UI plugin and used it to create a Graduate post type with just a few mouse clicks. Note that I did not check “Custom Fields” as a supported item even though I am using a couple of custom fields. Supporting custom fields adds support for the custom fields dialogue section on the add/edit page of the dashboard. I can still add/edit custom field values with code, which you will see later.

Creating a new custom post type

Below are the some of the advanced options selecting what your new post type supports.

Advanced Options

You can also create custom taxonomies with this plugin.

Creating the Custom Plugin

In my /wp-content/plugins/ folder, I created a .php file to store my plugin code. Note that if you do this, you need to give the file a very unique name for its function and your site so you avoid conflicting with any plugins you install later. I start this file with plugin header text.

/*
Plugin Name: UNCOAA Graduates
Description: Creates custom post type "Graduate" to store graduate info (name,username,year graduated); short code to list graduates; admin page setup
Version: 1.0
Author: David Sullivan
Author URI: http://davidsullivan.net/
*/

Shortcode for Data Display

I tailored Mark Jaquith’s code to fit my needs and added it to my plugin file. This code allows me to simply put the shortcode [graduates] on any page and have my data list displayed.

// [graduates] list of graduates Short code
function uncoaa_graduate_list() {
	global $post;
	$old_globals = $GLOBALS;
	$graduates = new WP_Query(
		array(
			'post_type' => 'graduate',
			'posts_per_page' => 50,
			'orderby' => 'meta_value', 'meta_key' => 'uncoaa_year_graduated',
			'order' => 'ASC'
			)
	);
	$return = '';
	uncoaa_css_hack();
	while ( $graduates->have_posts() ): $graduates->the_post();
		$uncoaa_username = get_post_meta($post->ID, 'uncoaa_username', true);
		$uncoaa_year_graduated = get_post_meta($post->ID, 'uncoaa_year_graduated', true);
		if ( username_exists( $uncoaa_username ) ) :
			$userID = username_exists( $uncoaa_username );
		else :
			$userID = 0;
		endif;
		$return .= "
";
		if(function_exists('userphoto_the_author_photo'))
		{
			$return .= userphoto__get_userphoto($userID, USERPHOTO_FULL_SIZE, "", "", array(), "");
		}
		$return .= "
"; $return .= get_the_title(); $return .= " - ". $uncoaa_year_graduated . "
";
		$return .= get_the_author_meta( 'user_description', $userID );
		$return .= "
";
		$return .= "";
	endwhile;
	$GLOBALS = $old_globals;
	return $return;
}
// end [graduates] list of graduates Shortcode
Screen shot shortcode on page Graduates listed on a page

Now if I change themes, this page will still display my graduate list without requiring me to create a custom page template.

Customizing the Admin Panel

Next, I wanted to customize the Admin panel when adding or editing my graduates so that my custom fields would show up just how I wanted them. Devin Price has some great tips for this.

Admin Panel for Graduates

Notice above the format that my custom field “Year Graduated” is in.

One Less Plugin

After displaying some of this work at a local WordPress Meetup, a comment was made about limiting the number of plugins to limit server overhead. I thought about the Custom Post Type UI plugin’s feature to “get code” that displays the code to generate the types and taxonomies you created with the plugin. I copied my graduate type’s code and pasted that into my custom plugin. Then I deactivated the CPTUI plugin. So now, all the work for my own custom post types is done solely in my one custom plugin. If I ever need to edit settings for the graduate type, I could just activate CPTUI, do the edits, get code and paste to my plugin and then deactivate CPTUI again.

Activate My Plugin

All that is left now is to activate my plugin. Then on any page where I want to display my Graduate data, I just drop in the [graduates] shortcode. If I create editor or administrator role users, they can easily go in to the dashboard and add new graduates.

Activating my Custom Plugin

One other note: I had a list of over 300 graduates to add. I was not about to attempt entering all these by and. Instead, I used the CSV Importer plugin seen above to import the list right into WordPress as a Graduate post type including my custom fields.

In Summary

To accomplish what I did, you can do these steps:

  • create a my_unique_file_name.php file in the /wp-content/plugins/ folder
  • add in appropriate plugin header text
  • add code to register your custom post types
  • add code to create a shortcode that you can use to display your data on pages
  • if you wish, add code to customize the admin panel for your post types
  • activate your custom plugin

I hope this helps someone or inspires something even better.

Peace and long life

      

1 Comment

Leave a reply