WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Custom field multiple checkboxes

[Resolved] Custom field multiple checkboxes

Viewing 12 replies - 1 through 12 (of 12 total)
  • It is this piece of code that does echoe all checkboxes, but does not save checked state nor store checked options in the database:

    case "checkboxes": {
    // Checkboxes Technology
    echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
    foreach($customField[ 'options' ] as $value){
    if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" ) $checked = "checked=\"checked\"";
    echo  '<input style="width: auto;margin:0 5px;" type="checkbox" name="'. $this->prefix .$value. '"' .$checked.'">' .$value . '</input>'."\n";}
    break;
    }

    Why is this? I tried adding square brackets at several places – as the once I thought were logical did not work – as Jason suggested, but no luck yet..

    Anybody here who could shed some light on things I am missing?

    latest code in which the multiple check boxes do not work is here: http://pastebin.com/Z65LkKVH Still not working/not being stored. Considering several cases, one per checkbox as a final resort, but would prefer to keep this array as is and get it working.

    Some feedback from IRC:

    the value should be as an attribute (value=”my value”) within the <input value=”my value” type=”checkbox” /> not <input type=”checkbox”>my value</input>

    Latest update http://pastebin.com/txtLUHqC

    Latest trial http://pastebin.com/BAftmUUJ . Anyone?

    Using

    case "checkboxes": {
     // Checkboxes Technology
    echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
    foreach($customField[ 'options' ] as $value){
    if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" ) $checked = "checked=\"checked\"";
    //if (isset($_POST[$customField]) == true ) $checked = "checked=\"checked\"";
    echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="'.$customField['name'] . '"' .$checked.'">' .$value . '</input>'."\n";}
    break;
    }

    I get
    Notice: Undefined variable: checked in /var/www/vhosts/domain.com/subdomains/lab/httpdocs/q3/wp-content/themes/d3/functions.php on line 392
    in debug mode. That line has

    echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="'.$customField['name'] . '"' .$checked.'">' .$value . '</input>'."\n";}

    So the variable is not registered. Maybe I should use an if else?

    OK, made some changes and variable is set now, bit if statement still does not store the checked options into the database nor returns the check marks:

    case "checkboxes": {
    // Checkboxes Technology
    echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
    foreach($customField[ 'options' ] as $value){
    echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="'.$customField['name'] . '"';
    if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" ) echo ' checked="checked"';
    echo  '>' .$value . '</input>'."\n";};
    break;

    Tried to check if a value was entered using isset, but got this error:

    Warning: Illegal offset type in isset or empty in /var/www/vhosts/domain.com/subdomains/lab/httpdocs/q3/wp-content/themes/d3/functions.php  on line 392

    Code used:

    case "checkboxes": {
     // Checkboxes Technology
    echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
    foreach($customField[ 'options' ] as $value){
    echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="'.$customField['name'] . '"';
    //if ( get_post_meta( $post->ID, $this->prefix . $customField['options'], true ) == "yes" ) echo ' checked="checked"';
    if (isset($_POST[$customField]) == true ) echo ' checked="checked"';
    echo  '>' .$value . '</input>'."\n";};				break;
    }

    Made several changes to the saving part, added another if to check for and store data from the options array. No error, but nothing stored either: http://pastebin.com/yGaUFZZM

    Latest code http://pastebin.com/Gxppbbjk. Added custom fields to posts again, but still have not solved the multiple checkbox issue.

    Well with some help I managed to solve the multiple checkbox issue.

    case "checkboxes": {
    // Checkboxes Technology  technologie
    echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
    $posted_values = get_post_meta( $post->ID, $this->prefix . $customField['name'], true);
    foreach($customField[ 'options' ] as $value){
     echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="' .  $this->prefix .$customField['name'] .'[]"';								if(is_array($posted_values)){
    if(in_array($value, $posted_values)){
    echo ' checked="checked"';
    }
    }

    Will publish the full code on one of my sites later on.

    no checked here spent days to get the echo checked — so if you have the code can you please post it?

    Thanks!!!!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Resolved] Custom field multiple checkboxes’ is closed to new replies.
Skip to toolbar