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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Theme Options - Tick list

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" => "Activity Purpose",
"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

This question has been answered.

Jennie Routley | 02/08/13 at 11:53am Edit
Tutorial: How to assign prize money

Previous versions of this question: 02/08/13 at 12:00pm

(8) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    02/08/13
    12:01pm
    Arnav Joy says:

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




    }

    }



    }



  • avatar
    Last edited:
    02/08/13
    1:23pm
    Christianto says:

    Hi Jennie,
    Please try this:

    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"){
    //new multiselect
    echo'<div class="multicheckbox">';
    $saved_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
    foreach($metabox["options"] as $option){
    $checked = (in_array($option, $saved_value)) ? 'checked="checked"' : '';
    echo'<span><input type="checkbox" name="'.$meta_box['name'].'_value[]" value="'.$option.'" '.$checked.' />'.$option.'</span>';
    }
    echo'</div>';
    //end new multiselect
    }
    }
    }


    This is multicheckbox,
    The submitted value $_POST['trans_value'] will be an array of all checked option

    Previous versions of this answer: 02/08/13 at 1:23pm

  • avatar
    Last edited:
    02/08/13
    12:17pm
    Jennie Routley says:

    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

    attachment image expert uploaded image

  • avatar
    Last edited:
    02/08/13
    12:29pm
    Arnav Joy says:

    check 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" ) {

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

    echo'<input type="checkbox" name="'.$meta_box['name'].'_value" ';

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

    echo ' selected="selected"';

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

    echo ' checked="checked"';

    }

    echo' />';


    }


    }

    }


    }

  • avatar
    Last edited:
    02/08/13
    12:30pm
    Arnav Joy says:

    <?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" ) {

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

    echo'<input type="checkbox" name="'.$meta_box['name'].'_value[]" ';

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

    echo ' selected="selected"';

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

    echo ' checked="checked"';

    }

    echo' />';


    }


    }

    }


    }

  • avatar
    Last edited:
    02/08/13
    1:14pm
    Jennie Routley says:

    Thanks both :) These are now bringing up the list of options to tick, the problem I am having now is that it doesn't seem to be saving the result.

    Any ideas?

    Thanks

  • avatar
    Last edited:
    02/08/13
    1:20pm
    Jennie Routley says:

    This appears to be the save function


    foreach($new_meta_boxes_2 as $meta_box) {

    // Verify
    if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
    return $post_id;
    }

    if ( 'page' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ))
    return $post_id;
    } else {
    if ( !current_user_can( 'edit_post', $post_id ))
    return $post_id;
    }

    $data = $_POST[$meta_box['name'].'_value'];

    if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
    add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
    elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
    update_post_meta($post_id, $meta_box['name'].'_value', $data);
    elseif($data == "")
    delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
    }


    Any idea why it snot saving the multicheck?

  • avatar
    Last edited:
    02/08/13
    1:28pm
    Christianto says:

    I update my answer, please check it.
    the value should be there saved correctly,
    I forgot to give attribute "checked" if the value exists in array.

This question has expired.



Jennie Routley voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.