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;
} ?>
<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.checked
attribute should be set ifin_array( $option, $value )
. (If you use@Jan
in your comment I'll get a notification)