Thursday, September 13, 2012

Multiple Metaboxes for Wordpress

0 comments
Using array() we can create a multiple metaboxes
    $meta_boxes = array();
     
        $meta_boxes = array(
    array(
    'id' => 'meta-box-1',
    'title' => 'meta box 1',
    'pages' => array('post', 'page', 'link'), // multiple post types
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
    array(
    'name' => 'Text box',
    'desc' => 'Enter something here',
    'id' => $prefix . 'text',
    'type' => 'text',
    'std' => 'Default value 1'
    )
    )
    ),
    array(
    'id' => 'meta-box-2',
    'title' => 'meta box 2',
    'pages' => array('post', 'link'), // custom post type
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
    array(
    'name' => 'Select box',
    'id' => $prefix . 'select',
    'type' => 'select',
    'options' => array('Option 1', 'Option 2', 'Option 3')
    )
    )
    )
    );
Using mytheme_show_box, we cannot handle multiple data we can create one metaboxes at a time.using classes we can overcome this problem.
    foreach ($meta_boxes as $meta_box) {
    $my_box = new My_meta_box($meta_box);
    }
    class My_meta_box {
     
    protected $_meta_box;
     
    // create meta box based on given data
    function __construct($meta_box) {
    $this->_meta_box = $meta_box;
    add_action('admin_menu', array(&$this, 'add'));
     
    add_action('save_post', array(&$this, 'save'));
    }
     
    /// Add meta box for multiple post types
    function add() {
    foreach ($this->_meta_box['pages'] as $page) {
    add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
    }
    }
     
    // Callback function to show fields in meta box
    function show() {
    global $post;
     
    // Use nonce for verification
    echo '';
     
    echo '';
     
    foreach ($this->_meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);
     
    echo '',
    '',
    '';
    }
     
    echo '
'; switch ($field['type']) { case 'text': echo '', '
', $field['desc']; break; case 'textarea': echo '', '
', $field['desc']; break; case 'select': echo ''; break; case 'radio': foreach ($field['options'] as $option) { echo '', $option['name']; } break; case 'checkbox': echo ''; break; } echo '
', '
'; } // Save data from meta box function save($post_id) { // verify nonce if (!wp_verify_nonce($_POST['mytheme_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 ($this->_meta_box['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); } } } }

Read more...