Making your plugin meta boxes work with custom post types in WordPress 3.0
Friday, 18th June 2010Getting 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.


June 19th, 2010 at 6:48 am
Great post Ben. I think custom post types are going to be a real challenge for the plugin community, and it’s probably going to be a good 12-18 months before we have universal support and/or a good solid set of conventions about how plugins interact with custom post types.
Custom Taxonomies often supported as well as they should be either….
July 2nd, 2010 at 1:18 pm
Thanks, I needed to do exactly this! I found that in WP 2.9.2, though, the code removes meta boxes due to the absence of $post_type->show_ui, so I’ve done this:
if ( !isset( $post_type->show_ui ) or $post_type->show_ui ) {
add_meta_box( 'robotsmeta', 'Robots Meta', array( 'RobotsMeta_Admin', 'noindex_option_fill' ), $post_type->name, 'side' );
}
September 25th, 2010 at 4:53 am
Perfect post for the solution I was looking for. You sir, are awesome!
September 25th, 2010 at 5:19 am
Is there a way that you can add meta boxes to the post types without having to modify the core plugin files? Perhaps through the use of filters and the themes’ function.php?
September 25th, 2010 at 9:04 am
Devin, you may just be able to add the add_meta_box() function into your theme’s functions.php file using the post type type you need. I havn’t tried this but you could give it a go…
add_action('add_meta_boxes', 'myplugin_add_custom_box'); function myplugin_add_custom_box() { add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'post_type','side'); }September 29th, 2010 at 12:27 am
Awesome, thanks a lot Ben!
September 29th, 2010 at 8:26 am
Devin, no problem, happy to help.
Any chance of a credit on your write up at WordImpressed.com?
November 18th, 2010 at 4:10 pm
Many, many thanks! I’ve been looking for this solution for a day now, and this is exactly what I’ve been trying to do – works like a charm!