Making your plugin meta boxes work with custom post types in WordPress 3.0

Getting to grips with custom post types in WordPress 3.0?

I am, and am finding they’re a great way to organise different content on your site and very easy to implement in just a few lines of code.

However, the challenge now is for plugin developers to make their plugins play nice with custom post types. The main thing I’ve noticed so far is that most plugins only tend to add meta boxes to posts and pages, so here’s the first step – getting your WordPress plugin meta boxes to work with custom post types.

A Real World Example

I needed to set some of my custom post type pages not to be indexed by search engines so I picked out the Robots Meta plugin by Joost de Valk – the best plugin for the job in my opinion – installed it, and found it was only showing a meta box when editing my posts and pages.

I’m going to use Joost’s plugin (version 3.3.1) to illustrate how you can get meta boxes to show for custom post types so download it and continue reading below.

The Meta Boxes

The file you’ll need to edit is robots-meta.php. The meta boxes are added to posts and pages by the meta_box function on line 24:

24
25
26
27
function meta_box() {
	add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'post','side');
	add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'page','side');
}

Meta Boxes for Custom Post Types

Firstly we’re going to check that the ‘get_post_types’ function exists. If it does then we’ll loop through all the post types and add meta boxes. If not we’ll continue just to add for posts and pages.

24
25
26
27
28
29
30
31
function meta_box() {
	if ( function_exists( 'get_post_types' ) ) {
		// Handle posts, pages and custom post types here
	} else {
		add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'post','side');
		add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'page','side');
	}
}

The next step is to get all the post types that have been registered – this will include the built-in post types for posts and pages. We then loop through them:

$post_types = get_post_types( array(), 'objects' );
foreach ( $post_types as $post_type ) {
	// Handle each post type here
}

Then for each post type we are going to add a meta box. It is import to note that when you add a custom post type you can choose wether or not to automatically create the WordPress UI in the admin. We only want to add meta boxes if the UI is generated by WordPress so we add in an ‘if’ statement to check.

24
25
26
if ( $post_type->show_ui ) {
	add_meta_box( 'robotsmeta', 'Robots Meta', array( 'RobotsMeta_Admin', 'noindex_option_fill' ), $post_type->name, 'side' );
}

The Finished Function

So put all that together and this is what you get:

24
25
26
27
28
29
30
31
32
33
34
35
36
function meta_box() {
	if ( function_exists( 'get_post_types' ) ) {
		$post_types = get_post_types( array(), 'objects' );
		foreach ( $post_types as $post_type ) {
			if ( $post_type->show_ui ) {
				add_meta_box( 'robotsmeta', 'Robots Meta', array( 'RobotsMeta_Admin', 'noindex_option_fill' ), $post_type->name, 'side' );
			}
		}
	} else {
		add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'post','side');
		add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'page','side');
	}
}

Please Note: This only deomstrates how you can add meta boxes for custom post types. There are other aspects such as checking user capabilities for different post types that may be applicable when you are saving your meta box data. See the capabilities arguments you can set in the register_post_type function.

getropin
 

Ben Huson