Logo Questions Linux Laravel Mysql Ubuntu Git Menu

WordPress: Custom Post Type, Send Data to "register_meta_box_cb" Arg




I'm adding multiple custom post types to a WordPress website, and I'm trying to optimize the code by using variables and merging functions. I was able to send 2 variables to the create_rma_project_post_type function that used to register_post_type. I'd like to do the same thing with the argument that attaches the metabox creation function. The first code below works completely. It uses register_meta_box_cb to call add_project_metaboxes:

add_action('init', create_rma_project_post_type('project','our-people'));
function create_rma_project_post_type($post_type,$display_page) {
    $prefix = 'rma';
    $post_label = ucfirst($post_type);

            'labels' => array(
                'name' => __( $post_label.'s' ),
                'singular_name' => __( $post_label ),
                'all_items' => __( $post_label.' List' ),
                'add_new' => __( 'Add New '.$post_label),
                'add_new_item' => __( 'Add New '.$post_label),
                'edit_item' => __( 'Edit '.$post_label),
                'new_item' => __( 'New '.$post_label),
                'view_item' => __( 'View '.$post_label),
                'search_items' => __( 'Search'),
                'parent_item_colon' => 'about-us/'.$display_page,
            'public' => true,
            'has_archive' => false,
            'rewrite' => array('slug' => 'about-us/'.$display_page,'with_front' => false),
            'supports' => array('title','editor','thumbnail'),
            'register_meta_box_cb' => 'add_project_metaboxes',

function add_project_metaboxes() {
    add_meta_box('rma_project_metabox_information', 'Project Information', 'rma_project_metabox_information_callback', 'rma_project', 'normal', 'default');

What I want to do is change the metabox call to send a variable, with the goal of being able to remove the "projects" word from the function, and use the one function for all custom post types.

This is what I'd like that line to read:

            'register_meta_box_cb' => add_project_metaboxes($post_type),

When I do that, it doesn't work. Neither do any of these:

            'register_meta_box_cb' => 'add_project_metaboxes($post_type)',
            'register_meta_box_cb' => function(add_project_metaboxes($post_type)),
            'register_meta_box_cb' => function('add_project_metaboxes($post_type)'),
            'register_meta_box_cb' => sprintf( __('add_project_metaboxes(%s)'), $post_type ),

My first question is, is this possible currently with WordPress? If yes, how can I do that? Appreciate any help, and if you need clarification of anything please let me know.

Edit 2017

Using this and other answers I created a set of helper classes to make CTP's easier to manage. If you'd like to see finished product, see:


like image 937
Andy Mercer Avatar asked Mar 20 '23 06:03

Andy Mercer

2 Answers

The register_meta_box_cb callback has one input argument, namely the WP_Post object of the currently edited post.

So I think you should be able to get the post type from that object by using the get_post_type() WordPress function.

If you check out the source code for the register_post_type() you will find that it contains this part:

 if ( $args->register_meta_box_cb )
     add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );

So try to replace:

'register_meta_box_cb' => 'add_project_metaboxes',


'register_meta_box_cb' => 'custom_add_metaboxes',


function custom_add_metaboxes( $post )

    // get the current post type
    $post_type = get_post_type( $post );

    // your logic for the add_meta_box code
    // ...


contains the logic based on the current post type.


is a way to store/associate an array of data to a custom post type?

a) Yes, you can use the update_post_meta() to store data associated to a custom post type with a given $post_id:

update_post_meta( $post_id, $meta_key, $meta_value );

where $meta_value can be a string or an array. For example:

update_post_meta( 38, 'my_data', array( 'somekey' => 'somevalue' ) );

To fetch the array data, you can use get_post_meta():

$data = get_post_meta( 38, 'my_data', FALSE );

b) If you want to store some data array, that's related to all posts in a given custom post types, you can use update_option() to store it and get_option() to fetch it.

For example you can use:

$data = array( 
              'cpt1' => array( 'somekey1' => 'somevalue1' ),
              'cpt2' => array( 'somekey2' => 'somevalue2' ),

update_option( 'my_data', $data );

to store the $data array and

$data = get_option( 'my_data' );

to fetch it back.


Hope this help.

like image 107
birgire Avatar answered Apr 06 '23 00:04


its not really a wordpress issue, more a question of how do functions get called when stored? if it is looking for a function name you need to give it as such.

I see what you are trying to do, if you want call add_project_metaboxes($post_type) just after you declare the array, add_meta_boxes used to be hooked to "init" so it may work. Btw...the function as it exists above won't work unless you modify it to take a parameter.

If not a OOP type object might be what you are looking for.

like image 35
David Avatar answered Apr 05 '23 23:04