web-dev-qa-db-fra.com

Problèmes avec la sauvegarde de metabox

J'avais donc quelques métaboxes, qui étaient mal étiquetées (meta_1, meta_2) et je les ai renommées pour les rendre plus faciles à comprendre pour la maintenance.

Lorsque je suis retourné voir comment d'autres utilisateurs développaient des métaboxes, j'ai trouvé ce style de création:

// 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' )
        )
    ),
)

Et la sortie dans la metabox est facile à suivre avec un $switch['type'] puis en bouclant la classe de tableau (par exemple $meta_project_information_array).

Quand je vais le sauver cependant, je suis perplexe.

D'abord je fais:

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

Je suppose que mes questions sont les suivantes: 1. Pourquoi les informations ci-dessus ne sont-elles pas sauvegardées lorsque je saisis les données? 2. Comment puis-je ajouter le nonce à la section de sauvegarde? Chaque metabox a-t-il un nonce distinct ou est-il identique? Certains CPT ont plus d'une metabox. 3. Y a-t-il une meilleure façon de s'y prendre?

Pour le moment j'ai 3 fichiers php:

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
Ahhhhhhhhhhhhhdfgbv
There are different ways of saving the metaboxes.
for e.g. you can create 2 files 
1) test-spec.php
2) test-meta.php

Dans le fichier test-spec.php

<?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
));
?>

Dans le fichier test-meta.php

Supposons que je veuille sauvegarder le champ de texte

<?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>
1
1991wp