0

Hi guys I have a headache on this I make a multicheck with this tutorial wpshout.com/create-an-in-post-theme-options-meta-box-in-wordpress/ the problem is it doesn't save all checked value and it doesn't checked the checked value when editing post.

Here are my code

I add here an array for 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 ); } ?>

I add else statement for multi checkbox

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

I made a function for 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 } ?>

How can I do code with saving all value of checked box? thanks!

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;
}    ?>
4
  • I once answered a similar question. The problem is that you have to save multiple values, and thus your <input> name should end with []. You also have to decide how to store the values: as a serialized array (all values in one meta field) or as multiple meta fields.
    – Jan Fabry
    Commented Jan 7, 2011 at 14:40
  • Hi Jan thanks for the response.. .I tested your answer in link and it's working and it's save the multichecked in one metafield serialized PHP array.. .my problem is how can I retrieve the value from serialized PHP array? here is the screenshot i51.tinypic.com/ddgwag.jpg I used <?php $meta = get_post_meta($post->ID,'dbt_multicheck',TRUE); echo $meta; ?> but it show "array" word? Commented Jan 10, 2011 at 4:45
  • Indeed, the different values are in an array now, so your checked attribute should be set if in_array( $option, $value ). (If you use @Jan in your comment I'll get a notification)
    – Jan Fabry
    Commented Jan 10, 2011 at 8:27
  • @Jan: thanks but i didn't get it..what I want to show in display is the checked box value...for example i checked all and in display page should be "Apple, Banana, cherry".. . how this output possibly code.. to display the which in serialized array thanks Commented Jan 11, 2011 at 4:46

1 Answer 1

2

I took the example code you suggested and changed the following to get multicheck support:

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

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

And of course, in post_meta_boxes():

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

Now you can handle "multichecks" of the form you suggested:

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

Finally some code to prevent warnings (which you won't see unless you enable 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;
6
  • @Jan thanks for the get_meta_check() funtion it works perfectly... .I have update above on my save_meta_data() it have a problem with saving it doesn't save in serialize array come from multichecked box, do I miss something in my code? thank you jan for helping me to find solution on this.. . Commented Jan 12, 2011 at 7:52
  • @idontknowhow: You call stripslashes() on $data, you should not do that when it is an array. Make it if ( is_array( $data ) ) { $data = array_map( 'stripslashes', $data ); } else { $data = stripslashes( $data ); }.
    – Jan Fabry
    Commented Jan 12, 2011 at 8:13
  • @Jan thanks for the time, i will try it.. .i have another question about if ( in_array( $option, $values ) ) { echo ' checked="checked"'; } $values are not matching from $option, with that it not return echo 'checked="checked"'; thank you! Commented Jan 13, 2011 at 6:36
  • @idontknowhow: I have updated my answer with a complete solution that I tested myself. It corrects some small errors, but is largely based on what you wrote. If something is unclear, please let me know.
    – Jan Fabry
    Commented Jan 13, 2011 at 8:02
  • @Jan Fabry THANK YOU SO MUCH! It's works great.. .til next time! again THANK YOU SO MUCH! for helping to solve this..I have learn a lot from you thanks for the time.. Commented Jan 14, 2011 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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