1

So I used to have a few metaboxes, which were labelled poorly (meta_1, meta_2) and I went about renaming them to make it easier to understand for maintenance.

When I went back to see how other users were developing metaboxes I found this style of creation:

// Review field array
$prefix_review = 'meta_review_';
$meta_review_information_array = array(

    array(
        'label' =>  'Single checkbox',
        'id'    =>  $prefix_review . 'checkbox',
        'class' =>  '',
        'type'  =>  'checkbox'
    ),

    array(
        'label'=> 'Number input',
        'desc'  => '',
        'id'    => $prefix_review . 'number',
        'class' => '',
        'min' => '1900',
        'max' => '2100',
        'step' => '1',      
        'type'  => 'number'
    ),

    array(
        'label' =>  'Text input',
        'desc'  =>  '',
        'id'    =>  $prefix_review . 'text',
        'class' =>  '',
        'type'  =>  'text'
    ),

    array (
        'label' =>  'Checkbox group',
        'desc'  =>  '',
        'id'    =>  $prefix_review . 'checkboxes',
        'class' =>  '',
        'type'  =>  'checkbox_group',
        'options' => array (
            'one'   => array ( 'label' => 'Label one', 'value' => 'one' ),
            'two'   => array ( 'label' => 'Label two', 'value' => 'two' ),
            'three' => array ( 'label' => 'Label three', 'value' => 'three' )
        )
    ),

    array (
        'label' =>  'Dropdown',
        'desc'  =>  '',
        'id'    =>  $prefix_review . 'dropdown',
        'class' =>  '',
        'type'  => 'select',
        'options' => array (
            'one'   => array ( 'label' => 'Label one', 'value' => 'one' ),
            'two'   => array ( 'label' => 'Label two', 'value' => 'two' )
        )
    ),
)

// Project field array
$prefix_project = 'meta_project_';
$meta_project_information_array = array(

    array(
        'label' =>  'Single checkbox',
        'id'    =>  $prefix_project . 'checkbox',
        'class' =>  '',
        'type'  =>  'checkbox'
    ),

    array(
        'label'=> 'Number input',
        'desc'  => '',
        'id'    => $prefix_project . 'number',
        'class' => '',
        'min' => '1900',
        'max' => '2100',
        'step' => '1',      
        'type'  => 'number'
    ),

    array(
        'label' =>  'Text input',
        'desc'  =>  '',
        'id'    =>  $prefix_project . 'text',
        'class' =>  '',
        'type'  =>  'text'
    ),

    array (
        'label' =>  'Checkbox group',
        'desc'  =>  '',
        'id'    =>  $prefix_project . 'checkboxes',
        'class' =>  '',
        'type'  =>  'checkbox_group',
        'options' => array (
            'one'   => array ( 'label' => 'Label one', 'value' => 'one' ),
            'two'   => array ( 'label' => 'Label two', 'value' => 'two' ),
            'three' => array ( 'label' => 'Label three', 'value' => 'three' )
        )
    ),

    array (
        'label' =>  'Dropdown',
        'desc'  =>  '',
        'id'    =>  $prefix_project . 'dropdown',
        'class' =>  '',
        'type'  => 'select',
        'options' => array (
            'one'   => array ( 'label' => 'Label one', 'value' => 'one' ),
            'two'   => array ( 'label' => 'Label two', 'value' => 'two' )
        )
    ),
)

And the output into the metabox is easy following through with a $switch['type'] then looping the array class (e.g. $meta_project_information_array).

When I go to save it though, I get stumped.

First I do:

add_action('save_post', '_save_metabox');

// Save the Data
function _save_metabox( $post_id ) {
    global $meta_review_information_array, $meta_project_information_array;

    // 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;
    }


    // Loop and save : review
    foreach( $meta_review_information_array as $meta_review_info ) {
        $meta_review_info_old = get_post_meta($post_id, $meta_review_info['id'], true);
        $meta_review_info_new = $_POST[$field_1['id']];
        if( $meta_review_info_new && $meta_review_info_new !=   $meta_review_info_old ) {
            update_post_meta( $post_id, $meta_review_info['id'], $meta_review_info_new );
        } elseif( '' == $meta_review_info_new && $meta_review_info_old ) {
            delete_post_meta($post_id, $meta_review_info['id'], $meta_review_info_old);
        }
    }

    // Loop and save: project
    foreach( $meta_project_information_array as $meta_project_info ) {
        $meta_project_info_old = get_post_meta($post_id, $meta_project_info['id'], true);
        $meta_project_info_new = $_POST[$field_1['id']];
        if( $meta_project_info_new && $meta_project_info_new != $meta_project_info_old ) {
            update_post_meta( $post_id, $meta_project_info['id'], $meta_project_info_new );
        } elseif( '' == $meta_project_info_new && $meta_project_info_old ) {
            delete_post_meta($post_id, $meta_project_info['id'], $meta_project_info_old);
        }
    }
}

I guess my questions are: 1. why doesn't the above save when I input the data? 2. how do I add the nonce to the saving section? Does each metabox have a separate nonce or all the same? Some CPT have more than one metabox. 3. is there a better way to go about this?

At the moment I have 3 php files:

admin_column.php  //this is the admin columns in the CPT

metabox.php //which has all the $types for the many metaboxes and the <table>

save_metabox.php //which is the saving of the metabox
1
There are different ways of saving the metaboxes.
for e.g. you can create 2 files 
1) test-spec.php
2) test-meta.php

In test-spec.php file

<?php
//DEFINE VARIABLE TO STORE THE METABOX
$content_test_meta = new WPAlchemy_MetaBox(array
(
'id' => '_content_test_meta',   //UNIQUE ID FOR THIS META BOX
'types' => array('post'), //LIMIT TO ONLY SHOW ON Default POST TYPE
'template' => get_stylesheet_directory() . '/metaboxes/test-meta.php'   //WHERE THE METABOX TEMPLATE IS FOR THIS METABOX
));
?>

In test-meta.php file

Suppose I want to save the text field

<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control metabox">

<label>Text Box</label>
<?php $metabox->the_field('text_box'); /* SET THE FIELD ID */ ?>
        <p><input type="text" name="<?php $metabox->the_name(); /* SET THE INPUT NAME TO THE FIELD ID */ ?>" value="<?php $metabox->the_value(); /* SET THE INPUT VALUE TO THE FIELD VALUE */ ?>" class="wp-editor-area" /></p>

</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.