Plugin “Meta Box”: Implementing meta boxes in custom post type

I am a WP beginner and see the potential of the “Meta Box” plugin (http://wordpress.org/plugins/meta-box/). I have already activated the plugin. So far I managed to upload the demo.php and to display the various meta boxes in the blog post window.

I have already read the documentation, but still I don’t know how to register the meta boxes for my custom post type. How do I implement them?

Read More

The code of my custom post type (it’s about listing artists) looks like this so far:

add_action('init', 'add_cpt_artists');
function add_cpt_artists() {
$labels = array(
      'name' => _x('Artists', 'post type general name'),
      'singular_name' => _x('Artist', 'post type singular name'),
      'add_new' => _x('Add', 'artist'),
      'add_new_item' => __('Add new artist'),
      'edit_item' => __('Edit artist'),
      'new_item' => __('New artist'),
      'view_item' => __('View artist'),
      'search_items' => __('Search for artist'),
      'not_found' =>  __('No artist found'),
      'not_found_in_trash' => 
      __('No artist in trash'),
      'parent_item_colon' => ''
   );
$supports = array('title', 'editor', 'thumbnail', 'excerpt');
$args = array(
      'labels' => $labels,
      'public' => true,
      'publicly_queryable' => true,
      'show_ui' => true, 
      '_builtin' => false,
      'show_in_menu' => true, 
      'query_var' => true,
      'rewrite' => array("slug" => "artists"),
      'capability_type' => 'post',
      'hierarchical' => false,
      'has_archive' => true, 
      'hierarchical' => false,
      'menu_position' => 10,
      'supports' => $supports
   );
register_post_type('artists',$args);
}

Let’s say from the Meta Box plugin I want to add two text areas and a box with checkboxes.

  • Where would I insert the code?
  • What would the code look like?
  • How can I retrieve the content in my page template?

Related posts

Leave a Reply

2 comments

  1. In this instance you would need to find the hook into the specific metabox and add your custom post type to its pages array, for example:

    $meta_boxes['test_metabox'] = array(
        'id' => 'test_metabox',
        'title' => 'Test Metabox',
        'pages' => array('post','page','artists'), //list custom post types here!
        'context' => 'normal',
        'priority' => 'high',
    

    The Meta Box plugin essentially adds a class that makes it easier to code metaboxes. You still need to actually register and code the metaboxes themselves into your theme’s files (see example below). To display only on your custom post type you need to modify the pages array (to add 'artists' to that array as shown above).

    Code example (not tested)

    Assuming you’ve got the plug in installed and activated, you should be able to add some text areas and checkbox meta boxes by pasting the following code to your themes functions.php file:

    add_filter( 'rwmb_meta_boxes', 't129292_register_meta_boxes' );
    
    function t129292_register_meta_boxes( $meta_boxes ) {
    
    $prefix = 'rw_';
    // Register the metabox
    $meta_boxes[] = array(
        'id'       => 'personal',
        'title'    => 'Personal Information',
        'pages'    => array( 'artists' ), //displays on artists post type only
        'context'  => 'normal',
        'priority' => 'high',
        'fields' => array(
    
            // add a text area
            array(
                'name'  => 'Text Area',
                'desc'  => 'Description',
                'id'    => $prefix . 'text1',
                'type'  => 'textarea',
                'cols' => 20,
                'rows' => 3,                
            ),
            // add another text area
            array(
                'name'  => 'Text Area',
                'desc'  => 'Description',
                'id'    => $prefix . 'text2',
                'type'  => 'textarea',
                'cols' => 20,
                'rows' => 3             
            ),
            // CHECKBOX
            array(
                'name' => __( 'Checkbox', 'rwmb' ),
                'id'   => $prefix . 'checkbox',
                'type' => 'checkbox',
                // Value can be 0 or 1
                'std'  => 1,
            )
        )
    );
    
    return $meta_boxes;
    
    }
    

    Then to display the options on the front-end in your theme files you can use the helper function, e.g.

    echo rwmb_meta( 'rw_text1' ); //echo the contents from the first textarea
    echo rwmb_meta( 'rw_text2' ); //echo the contents from the second textarea
    echo rwmb_meta( 'rw_checkbox' ); //echo 1 or 0 if the checkbox is checked
    

    These examples are modified from the plugin’s Github repo where the demo file (linked) contains code on using a variety of metabox types, not just textareas and checkboxes.