How to construct multiple meta boxes

As of now I have a perfectly working script for a single meta-box. (I’ll supply the details below.) However, I want to add more meta-boxes but am having trouble wrapping my head around how I would bend or add to this script to get those results.

I had someone explain to me that I should create individual include’s for each metabox and essentially recreate duplicates of what I already have, changing my functions and such for example: from plib_add_box to plib_add_box_2 and so on so they don’t clash. But that’s so crude and I know there has to be a way to simply add it to my existing include and essentially keep my format/save data portions the same.

Read More

my functions.php:

include("metaboxes/preset-library.php");

//I created an array called $meta_box and set the array key to the relevant post type, in this case post
$meta_box['post'] = array(
    'id' => 'venue_location',  
    'title' => 'Venue/Location',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Venue',
            'desc' => 'Venue Name',
            'id' => 'venue_info',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Location',
            'desc' => 'Location of the Venue',
            'id' => 'location_info',
            'type' => 'text',
            'default' => ''
        )
    )
);
add_action('admin_menu', 'plib_add_box');

my include:

//Add meta boxes to post types
function plib_add_box() {
    global $meta_box;

    foreach($meta_box as $post_type => $value) {
        add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
    }
}
//Formatting
function plib_format_box() {
  global $meta_box, $post;

  // verification
  echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

  echo '<table class="form-table">';

  foreach ($meta_box[$post->post_type]['fields'] as $field) {
      // get current post meta data
      $meta = get_post_meta($post->ID, $field['id'], true);

      echo '<tr>'.
              '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
              '<td>';
      switch ($field['type']) {
          case 'text':
              echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:97%" />'. '<br />'. $field['desc'];
              break;
          case 'textarea':
              echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
              break;
          case 'select':
              echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
              foreach ($field['options'] as $option) {
                  echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
              }
              echo '</select>';
              break;
          case 'radio':
              foreach ($field['options'] as $option) {
                  echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
              }
              break;
          case 'checkbox':
              echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
              break;
      }
      echo     '<td>'.'</tr>';
  }

  echo '</table>';

}
// Save data from meta box
function plib_save_data($post_id) {
    global $meta_box,  $post;

    //Verify
    if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    //Check > autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    //Check > permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    foreach ($meta_box[$post->post_type]['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}

add_action('save_post', 'plib_save_data');

Related posts

Leave a Reply

1 comment

  1. You can create as many metaboxes as you want. Use a function to create them then hook into init. The first line should be $meta_boxes = array(); then start each new metabox with $meta_boxes[] = array(

    add_action( 'init', 'prefix_create_metaboxes' );
    function prefix_create_metaboxes() {
    
    $meta_boxes = array();
    $meta_boxes[] = array(
        'id' => 'venue_location',  
        'title' => 'Venue/Location',
        'pages' => array('post'), // post type
        'context' => 'normal',
        'priority' => 'high',
         array(
            'name' => 'Venue',
            'desc' => 'Venue Name',
            'id' => 'venue_info',
            'type' => 'text',
            'default' => ''
        ),
        array(
            'name' => 'Location',
            'desc' => 'Location of the Venue',
            'id' => 'location_info',
            'type' => 'text',
            'default' => ''
        ),
    )
    );
    $meta_boxes[] = array(
        'id' => 'another_meta_box',
        'title' => 'Another Meta Box Title',
        'pages' => array( 'post' ), // post type
        'context' => 'side',
        'priority' => 'low',
        'show_names' => true, 
        'fields' => array(
        array(
            'name' => 'Field Name',
            'desc' => 'Field Desc.',
            'id' => 'field_id',
            'type' => 'text'
        ),
        )
        );
    }