0

I have a plugin I'm developing. As part of its functionality I need to set up an option group and specify an option var for that group:

function swcs_settings() {
  register_setting( 'swcs-settings-group', 'swcs_postpage_id' );
}
add_action( 'admin_init', 'swcs_settings' );

Where swcs_postpage_idis the name of my variable.

I am using this variable in a <select> box like so:

<select name="swcs_postpage_id" id="swcs_postpage_id" multiple>
<?php global $post; 
$args = array( 'numberposts' => -1); 
$posts = get_pages($args); 
foreach( $posts as $post ) : setup_postdata($post); ?>                
    <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>

As you can see I am using the <select> with the multiple attribute so a user can make more than one selection if they so desire.

What I'm struggling with is passing more than one value to swcs_postpage_id. I am guessing that the var really needs to be an array and not a var at all but I am not sure what to do in order to:

  1. Store multiple values in the option swcs_postpage_id
  2. Output and iterate through them using get_option('swcs_postpage_id');

Any help gratefully received.

1 Answer 1

1

It's a lot simpler than that:

<select name="swcs_postpage_id[]" id="swcs_postpage_id" multiple>

See those brackets? Boom.

0

Your Answer

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

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