This article came about sort of by accident. In a previous article, I explained how to create a custom post type to display content on a page other than my home page. Iâm still going to post to the home page, but I wanted a dedicated page for my Lead Like Jesus devotionals. As I started adding content to the newly created custom post type, I wanted to make sure the Sharedaddy feature was turned on for each post. Many of my readers enjoy sharing the thrice weekly devotional messages. They are uplifting, thought provoking and inspirational. But lo and behold, as I went to click the Sharedaddy checkbox to turn sharing on, the Sharedaddy meta-box was missing from my custom post type editor page! Obviously I missed something, but I didnât know what it is.
After a couple of hours of searching every corner of the internet for an article, any article, that would point me in the right direction, I finally found a snippet of information that briefly mentions adding code to a themeâs functions.php source code to show plug-in meta-boxes on custom post type editor pages. The article was neither clear nor complete enough for me to fully grasp what needs to be done. Yet, there was just enough information to get me started down the right track even though I had to figure almost everything out for myself. Hereâs what I did. It works and it works well.
Letâs continue to use Sharedaddy as our example. First, open the plugin editor and retrieve sharedaddy.php. Please view the video below for details on opening the pluginâs source file and copying the pertinent code to an external text editor. Weâll be editing the code copied from the source file and creating our own custom function.
Now that the code is copied into the external text editor, open your themeâs functions.php file and copy itâs entire source into your text editor as well. I recommend doing it this way, because at least on my computer, the TinyMCE editor used by WordPress is a bit quirky. Every time I attempt to edit a file, Iâll start writing and the code inside the text editor jumps either up or down and I lose sight of what Iâm doing. I could still type and the changes will take even though I canât see what I am doing. I donât know if this is a problem with the very problematic IE8 or whether the problem is with the editor itself. Iâll test the WordPress editor with Firefox later. Weâre going to modify the code we copied from Sharedaddy.php and hook it into the function we wrote for creating custom post types. If you recall, my custom function looks like this:
/** following code block added by vmf on 11/12/2010
* registers custom post type 'leadlikejesus'
* caution: if template gets updated by developer
* this file may get overwritten and you will have to restore
* this functionality. Make sure you save it someplace safe.
*/
// execute the create_post_type() function
add_action( 'init', 'create_post_type' );
function create_post_type() {
// create an array for all of the display prompts
$labels = array(
'name' => __( 'Devotionals' ),
'singular_name' => __( 'Devotional' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Devotional' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Devotional' ),
'new_item' => __( 'New Devotional' ),
'view' => __( 'View Devotional' ),
'view_item' => __( 'View Devotional' ),
'search_items' => __( 'Search Devotionals' ),
'not_found' => __( 'No devotionals found' ),
'not_found_in_trash' => __( 'No devotionals found in Trash' ),
'parent_item_colon' => ''
);
// create an array for the arguments to pass to register_post_type()
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'_builtin' => false, // It's a custom post type, not built in!
'rewrite' => array('slug' => 'devotionals'),
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'author', 'editor', 'comments', 'trackbacks', 'sticky', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes' )
);
// register the new custom post type
register_post_type( 'leadlikejesus', $args);
// flush the rewrite rules to prevent 404 error
flush_rewrite_rules();
}
// end custom post type 'leadlikejesus'
I've highlighted line 40 in the source code example. Make some room above line 40 to insert the following function between register_post_type() and flush_rewrite_rules():
register_post_type( 'leadlikejesus', $args);
// Hook things in, late enough so that add_meta_box() is defined
if (is_admin()){
add_action( 'admin_init', 'add_plugin_meta_boxes' );
add_action( 'save_post', 'save_sharing_box' );
}
flush_rewrite_rules();
In the block of code we've just inserted, add_action is calling a custom function called add_plugin_meta_boxes(). This new function simply calls the add_meta_box() we copied from the sharedaddy.php source file. Still with me? I know you are!
Now this is something I can't fully explain, but I think it's related to a timing issue. In my first draft of this code, I did not have the if (is_admin()){} wrapper around the two add_actions. For a reason I haven't discovered, the first ad_action() line kept causing php errors with the message that add_meta_box was undefined or not found or something along those lines. Once I inserted if (is_admin()){}, everything worked beautifully. Go figure!
Going back to our inserted code above, the second add_action is calling a function called save_sharing_box(). This is simply a renamed, but otherwise unedited version of the sharing_meta_box_save() function lifted out of sharedaddy.php. Our final code with all the new functionality is:
/** following code block added by vmf on 11/12/2010
* registers custom post type 'leadlikejesus'
* caution: if template gets updated by developer
* this file may get overwritten and you will have to restore
* this functionality. Make sure you save it someplace safe.
*** added: hooks into shareaddy by vmf on 11/15/2010
*/
add_action( 'init', 'create_post_type' );
function create_post_type() {
$labels = array(
'name' => __( 'Devotionals' ),
'singular_name' => __( 'Devotional' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Devotional' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Devotional' ),
'new_item' => __( 'New Devotional' ),
'view' => __( 'View Devotional' ),
'view_item' => __( 'View Devotional' ),
'search_items' => __( 'Search Devotionals' ),
'not_found' => __( 'No devotionals found' ),
'not_found_in_trash' => __( 'No devotionals found in Trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'_builtin' => false, // It's a custom post type, not built in!
'rewrite' => array('slug' => 'devotionals'),
'capability_type' => 'post',
'hierarchical' => false,
'taxonomies' => array('category', 'post_tag'),
'supports' => array( 'title', 'author', 'editor', 'comments', 'trackbacks', 'sticky', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes' )
);
register_post_type( 'leadlikejesus', $args);
// Hook things in, late enough so that add_meta_box() is defined
if (is_admin()){
add_action( 'admin_init', 'add_plugin_meta_boxes' );
add_action( 'save_post', 'save_sharing_box' );
}
flush_rewrite_rules();
}
// This function tells WP to add the sharing "meta box"
function add_plugin_meta_boxes() {
add_meta_box( 'sharing_meta', __( 'Sharing', 'sharedaddy' ), 'sharing_meta_box_content', 'leadlikejesus', 'advanced', 'high' );
// add_meta_box ('keyworddensitycheckerbox', __('Keyword Density Checker', 'keyword-density-checker'), array ('KeywordDensityChecker', 'post_keyword_density_checker'), 'leadlikejesus', 'normal', 'core');
}
function save_sharing_box( $post_id ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Record sharing disable
if ( 'leadlikejesus' == $_POST['post_type'] ) {
if ( current_user_can( 'edit_post', $post_id ) ) {
if ( isset( $_POST['sharing_status_hidden'] ) ) {
if ( !isset( $_POST['enable_post_sharing'] ) )
update_post_meta( $post_id, 'sharing_disabled', 1 );
else
delete_post_meta( $post_id, 'sharing_disabled' );
}
}
}
return $post_id;
}
// end custom post type 'leadlikejesus'
By the way, I finished editing this post on Firefox. TinyMCE is well behaved on this browser.