Pay money and get answers for your WordPress questions (more info)

Theme Options - Tick list

  • SOLVED

I have the below option field in my posts so that the user can select a particular item from a drop down. This works fine, however I need them to be able to choose more than one option, so I would like a multi-select option.

Do you know how I would go about creating a list of tick boxes then they can select more than one item?

Code for the option as it stands currently is below.


"trans" => array(
"name" => "trans",
"type" => "multicheck",
"options" => array("Indoor", "Outdoor", "Ice Breaker", "Evening"),
"std" => "",
"title" => "<strong>Activity Purpose</strong>",
"description" => "Select which purpose category this activity fits into"),


I then have this lower in the page for I assume displaying and saving the options, I see I have one for input, textarea, and select, but what would I need to put for a multiselect?



function new_meta_boxes_2() {
global $post, $new_meta_boxes, $new_meta_boxes_2;

foreach($new_meta_boxes_2 as $meta_box) {

echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';

echo'<h2 class="'.$meta_box['class'].'"">'.$meta_box['title'].'</h2>';

if( $meta_box['type'] == "input" ) {

$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
$meta_box_value = str_replace('"', "'", $meta_box_value);

echo'<input type="text" name="'.$meta_box['name'].'_value" class="'.$meta_box['class'].'" value="'.$meta_box_value.'" size="55" /><br />';

} elseif( $meta_box['type'] == "textarea" ) {

$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);

if($meta_box_value == "")
$meta_box_value = $meta_box['std'];

echo'<textarea name="'.$meta_box['name'].'_value" class="'.$meta_box['class'].'" value="'.$meta_box_value.'" style="width:100%" cols="20" rows="7">'.$meta_box_value.'</textarea><br />';

} elseif ( $meta_box['type'] == "select" ) {

echo'<select name="'.$meta_box['name'].'_value">';

foreach ($meta_box['options'] as $option) {

echo'<option';
if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {
echo ' selected="selected"';
} elseif ( $option == $meta_box['std'] ) {
echo ' selected="selected"';
}
echo'>'. $option .'</option>';

}

echo'</select>';

//new multiselect


STUFF IN HERE

//end new multiselect
}
}

}



Many thanks!
Badger

Answers (2)

2013-02-08

Arnav Joy answers:

try this
<?php
function new_meta_boxes_2() {

global $post, $new_meta_boxes, $new_meta_boxes_2;



foreach($new_meta_boxes_2 as $meta_box) {



echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';



echo'<h2 class="'.$meta_box['class'].'"">'.$meta_box['title'].'</h2>';



if( $meta_box['type'] == "input" ) {



$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);



if($meta_box_value == "")

$meta_box_value = $meta_box['std'];

$meta_box_value = str_replace('"', "'", $meta_box_value);



echo'<input type="text" name="'.$meta_box['name'].'_value" class="'.$meta_box['class'].'" value="'.$meta_box_value.'" size="55" /><br />';



} elseif( $meta_box['type'] == "textarea" ) {



$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);



if($meta_box_value == "")

$meta_box_value = $meta_box['std'];



echo'<textarea name="'.$meta_box['name'].'_value" class="'.$meta_box['class'].'" value="'.$meta_box_value.'" style="width:100%" cols="20" rows="7">'.$meta_box_value.'</textarea><br />';



} elseif ( $meta_box['type'] == "select" ) {



echo'<select name="'.$meta_box['name'].'_value">';



foreach ($meta_box['options'] as $option) {



echo'<option';

if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {

echo ' selected="selected"';

} elseif ( $option == $meta_box['std'] ) {

echo ' selected="selected"';

}

echo'>'. $option .'</option>';



}



echo'</select>';




}


elseif ( $meta_box['type'] == "multicheck" ) {



echo'<select name="'.$meta_box['name'].'_value" multiple="multiple">';



foreach ($meta_box['options'] as $option) {



echo'<option';

if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {

echo ' selected="selected"';

} elseif ( $option == $meta_box['std'] ) {

echo ' selected="selected"';

}

echo'>'. $option .'</option>';



}



echo'</select>';




}

}



}




Jennie Routley comments:

Ooh thanks Arnav, that is close, but its not giving me tick boxes just them in a list and I am only able to select one.
See screenshot.

Many thanks