Question

Salut les gars, j'ai un mal de tête sur ce que je fais un multicheck avec ce tutoriel wpshout.com/create-an-in-post-theme-options-meta-box-in-wordpress/ le problème est-il ne sauvegarde tout vérifié la valeur et il n'a pas vérifié la valeur vérifiée lors de l'édition après.

Voici mon code

J'ajouter ici un tableau pour muticheckbox

    <?php    function hybrid_post_meta_boxes() {

/* Array of the meta box options. */
$meta_boxes = array(
    'title' => array( 'name' => 'Title', 'title' => __('Title', 'hybrid'), 'type' => 'text' ),
    'description' => array( 'name' => 'Description', 'title' => __('Description', 'hybrid'), 'type' => 'textarea' ),
    'location' => array( 'name' => 'location', 'title' => __('Location:', 'hybrid'), 'type' => 'check', 'options' => array(
    'AL' => 'Alabama',
    'CA' => 'California',
    'DE' => 'Delaware',
),
);

return apply_filters( 'hybrid_post_meta_boxes', $meta_boxes ); } ?>

ajouter instruction else multi case à cocher

    <?php
    function post_meta_boxes() {
    global $post;
    $meta_boxes = hybrid_post_meta_boxes(); ?>

<table class="form-table">
<?php foreach ( $meta_boxes as $meta ) :

    $value = get_post_meta( $post->ID, $meta['name'], true );

    if ( $meta['type'] == 'text' )
        get_meta_text_input( $meta, $value );
    elseif ( $meta['type'] == 'textarea' )
        get_meta_textarea( $meta, $value );
    elseif ( $meta['type'] == 'select' )
        get_meta_select( $meta, $value );
    elseif ( $meta['type'] == 'check' )
        get_meta_check( $meta, $value );

endforeach; ?>
</table>

J'ai fait une fonction pour multicheck

    <?php function get_meta_check( $args = array(), $value = false ) {

extract( $args ); ?>

<tr>
    <th style="width:10%;">
        <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
    </th>
    <td>

        <?php foreach ( $options as $option ) : ?>
          <input type="checkbox" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value=", <?php echo $option; ?>, " <?php if ( htmlentities( $value, ENT_QUOTES ) == $option ) echo ' checked="checked"'; ?> /> <?php echo $option; ?>,
        <?php endforeach; ?>
        <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </td>
</tr>
<?php } ?>

Comment puis-je faire un code de sauver toute valeur de case cochée? Merci!

  
    

UPDATE

  
    function save_meta_data( $post_id ) {
    global $post;

    if ( 'page' == $_POST['post_type'] )
        $meta_boxes = array_merge( page_meta_boxes() );
    else
        $meta_boxes = array_merge( post_meta_boxes() );

    foreach ( $meta_boxes as $meta_box ) :

        if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
            return $post_id;

        if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) )
            return $post_id;

        elseif ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
            return $post_id;

        $data = stripslashes( $_POST[$meta_box['name']] );
        if ( get_post_meta( $post_id, $meta_box['name'] ) == '' )
            add_post_meta( $post_id, $meta_box['name'], $data, true );

        elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) )
            update_post_meta( $post_id, $meta_box['name'], $data );

        elseif ( $data == '' )
            delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) );

    endforeach;
}    ?>
Était-ce utile?

La solution

Je pris le code exemple, vous suggérez et modifié ce qui suit pour obtenir de l'aide multicheck:

// Add this at the end of the file
// A good prefix for your name prevents problems later!
function wpse6513_get_meta_check( $args = array(), $current_values = array() )
{
    extract( $args );

    $name_esc = esc_attr( $name );

    if ( ! is_array( $current_values ) ) {
        $current_values = array();
    }

    echo '<tr>';
    echo '<th style="width: 10%">';
    echo '<label for="' . $name_esc . '">' . esc_html( $title ) . '</label>';
    echo '</th>';
    echo '<td>';

    foreach ( $options as $option_value => $option_label ) {
        $option_value_esc = esc_attr( $option_value );
        echo '<label><input type="checkbox" name="' . $name_esc . '[]" id="' . $name_esc . '_' . $option_value_esc . '" value="' . $option_value_esc . '"';
        if ( in_array( $option_value, $current_values ) ) {
            echo ' checked="checked"';
        }
        echo '/> ' . esc_html( $option_label );
        echo '</label><br/>';
    }
    echo '<input type="hidden" name="' . $name_esc . '_noncename" id="' . $name_esc . '_noncename" value="' . wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />';

    echo '</td>';
    echo '</tr>';
}

En hybrid_save_meta_data():

// Replace this line:
$data = stripslashes( $_POST[$meta_box['name']] );
// By these lines:
$data = array_key_exists( $meta_box['name'], $_POST ) ? $_POST[$meta_box['name']] : '';
if ( is_array( $data ) ) {
    $data = array_map( 'stripslashes', $data );
} else {
    $data = stripslashes( $data );
}

Et bien sûr, dans post_meta_boxes():

// Add this to the `$meta['type']` if/else list
elseif ( $meta['type'] == 'check' )
    wpse6513_get_meta_check( $meta, $value );

Maintenant, vous pouvez gérer « multichecks » du formulaire suggéré:

'location' => array(
    'name' => 'location',
    'title' => __('Location:', 'hybrid'),
    'type' => 'check',
    'options' => array(
        'AL' => 'Alabama',
        'CA' => 'California',
        'DE' => 'Delaware',
    ),
),

Enfin un peu de code pour éviter les avertissements (que vous ne verrez moins que vous ne WP_DEBUG):

// Replace all `wp_specialchar()` calls with `esc_attr()`
// Add these lines in `hybrid_save_meta_data()`:
// At the start, after `global $post`:
if ( ! array_key_exists( 'post_type', $_POST ) ) {
    return $post_id;
}

// In the foreach loop, instead of:
if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
    return $post_id;
// Write:
$nonce_name = $meta_box['name'] . '_noncename';
if ( ! array_key_exists( $nonce_name, $_POST ) || !wp_verify_nonce( $_POST[$nonce_name], plugin_basename( __FILE__ ) ) )
    return $post_id;
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top