• Skip to main content

Victor Font Consulting Group, LLC

The DEX Intranet Specialists

Call Us:

+1 919-604-5828

  • Home
  • Care Plans
    • Care Articles
    • Optional Subscriptions
  • Consultations
  • Products
    • Code Snippets
    • Public GitHub Repositories
    • Gist Snippets
    • Pastebin Snippets (Free)
    • Free Plugins
  • FAQs
  • Support
    • Graphic Design
  • Contact
    • Speakers
    • Portfolio
  • Resources
    • Free WordPress Video Training
    • Tutorials
    • Articles
    • Cybersecurity
    • EU Referral Network
You are here: Home / Code Snippet / How to Add Plug-in Meta Boxes to WordPress Custom Post Type Editor Pages

How to Add Plug-in Meta Boxes to WordPress Custom Post Type Editor Pages

By Victor M. Font Jr.
November 19, 20101 Comment

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.

  • 0share
  • Facebook0
  • Twitter0
  • Pinterest0
  • LinkedIn0
  • Print
  • SMS0

About Victor M. Font Jr.

Victor M. Font Jr. is an award winning author, entrepreneur, and Senior IT Executive. A Founding Board Member of the North Carolina Executive Roundtable, he has served on the Board of Advisors, of the North Carolina Technology Association, the International Institute of Business Analysis, Association of Information Technology Professionals, Toastmasters International, and the North Carolina Commission for Mental Health, Developmental Disabilities, and Substance Abuse Services. He is author of several books including The Ultimate Guide to the SDLC and Winning With WordPress Basics, and Cybersecurity.

Reader Interactions

VictorFont.com runs on the Genesis Framework

Genesis FrameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Check out the incredible features and the selection of designs. It's that simple—start using Genesis now!

Click here to download The Genesis Guide for Absolute Beginners (PDF - 1.4 MB)

Leave a Reply Cancel reply

Your email address and website will not be published. Required fields are marked *
Posting a comment means that you agree with and accept our Comment & Product Review Policy

Comments

  1. Patrick

    August 23, 2011 at 6:07 pm

    Hi,
    I have exactly the same problem – how to get plugin metaboxes to show in custom post types – who would of thought it could be so difficult. In any case, I would like to show the metaboxes associated with the plugins WP Customer Reviews and Geotag to show in my Places custom post type. I tried Geotag first. I’ve found the add_meta_box function but am having problems where to go next…I’m sure the principle is the same as the one you used…just don’t know it as yet…any suggestions would be great!

    Reply

Call: +1 919-604-5828

Send us an E-mail

Accessibility Statement | Affiliate Marketing Disclosure | Capability Statement

Cookie Policy | Comment & Product Review Policy | Privacy Policy | Site Map | Terms & Conditions

Copyright © 2003–2023 Victor M. Font Jr.

Return to top of page