Ask your WordPress questions! Pay money and get answers fast! (more info)

Checkbox multiple in option page WordPress

  • SOLVED

Hi,

I just want to know how can I achieve a multiple checkbox combining settings API and get_pages(). I want to have some page listing.

For now I got something like this:

$pages = get_pages('sort_column=post_parent,menu_order');
$value = get_option('my_settings');
foreach ( $pages as $page ) :
$checked = '';
$checked = ( $page->ID == $value ) ? 'checked="checked"': '';?>
<label><input type="checkbox" name="my_settings[]" value="<?php echo $page->ID; ?>" <?php echo $checked; ?> /> <?php echo $page->post_title; ?></label><br />
<?php endforeach;


But I just have one value if I echo my option. Any hint?

Answers (2)

2013-09-27

Expert answers:

<?php
$pages = get_pages('sort_column=post_parent,menu_order');
$value = maybe_unserialize( get_option('my_settings') );
foreach ( $pages as $page ) :
$checked = '';
if( is_array( $value ) ) {
$checked = in_array($page->ID, $value ) ? 'checked="checked"' : '';
} else {
$checked = ( $page->ID == $value ) ? 'checked="checked"': '';
}
?>
<label>
<input type="checkbox" name="my_settings[]" value="<?php echo $page->ID; ?>" <?php echo $checked; ?> />
<?php echo $page->post_title; ?>
</label>
<br />
<?php
endforeach;
?>


Clothilde comments:

Thanks, the checked is showing up again but I still get only one value, I want multiple choices.


Expert comments:

Can you replace this line with below code and see if it prints an array ??
$value = maybe_unserialize( get_option('my_settings') );

Debug code:
$value = maybe_unserialize( get_option('my_settings') );
print_r( $value );


Else provide here the full code of yours, so we can have a better understanding of form processing.


Expert comments:

Yes, paste the code of this callback function " my_settings_callback " here.


Clothilde comments:

I've post[[LINK href="http://pastebin.com/iUSNLPXb"]] here[[/LINK]]


Clothilde comments:

Here is the code :

function my_settings_callback($inputs)
{
if( !isset( $_POST['settings_nonce'] ) || !wp_verify_nonce( $_POST['settings_nonce'], 'my_settings_nonce' ) ) return;
return $inputs;
}


Clothilde comments:

I need this to work to be used in a is_page() condition. Would be nice if you can do this.


Expert comments:

<?php
switch ( $args['type'] ) :
case 'checkbox' :
?>
<legend class="screen-reader-text"><span><?php echo $args['label_screen']; ?></span></legend>
<?php
$pages = get_pages();
$value = maybe_unserialize( get_my_option($args['name'], '' ) );
foreach ( $pages as $page ) :
$checked = '';
if( is_array( $value ) ) {
$checked = in_array($page->ID, $value ) ? 'checked="checked"' : '';
} else {
$checked = ( $page->ID == $value ) ? 'checked="checked"': '';
}
?>
<label>
<input type="checkbox" id="<?php echo $args['name']; ?>" name="my_settings[<?php echo $args['name']; ?>][]" value="<?php echo $page->ID; ?>" <?php echo $checked; ?>/>
<?php echo $page->post_title; ?>
</label>
<br />
<?php endforeach;
echo $description; ?>
<?php
break;


Expert comments:

Can you check with the above code ?
This is the change done : name="my_settings[<?php echo $args['name']; ?>][]"


Clothilde comments:

You're the best !


Expert comments:

Please upvote my answer !! :)


Clothilde comments:

I can't ! Sorry

2013-09-27

Arnav Joy answers:

try this

<?php

$pages = get_pages('sort_column=post_parent,menu_order');

$value = get_option('my_settings');

foreach ( $pages as $page ) :

$checked = '';

$checked = ( @in_array( $page->ID , $value ) ) ? 'checked="checked"': '';?>

<label><input type="checkbox" name="my_settings[]" value="<?php echo $page->ID; ?>" <?php echo $checked; ?> /> <?php echo $page->post_title; ?></label><br />

<?php endforeach;

?>


Clothilde comments:

Thanks but I'm still getting the same, only the last value and nothing is checked now.


Arnav Joy comments:

can you show full code of how you are saving the value?


Clothilde comments:

It's a generic function I made to iterate fields in my callback => case 'checkbox', case 'textarea', etc. I'm saving all values in one function :

add_action( 'admin_init', 'my_register_setting' );
function my_register_setting()
{
register_setting( 'my_slug', my_settings', 'my_settings_callback' );
}


Clothilde comments:

Here is the function @Arnav :
[[LINK href="http://pastebin.com/iUSNLPXb"]]pastebin code[[/LINK]]