WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom field multiple checkboxes (13 posts)

  1. Rhand
    Member
    Posted 4 years ago #

    I am working on several custom fields based and a custom type post based
    upon information from two sources which I indicated in the code @
    http://wordpress.pastebin.com/TAL2C0DX . All custom fields *except* my
    multiple checkboxes field - line 163 - 172 - are properly stored in the
    database and returned in the Dashboard. Does anyone know how I can remedy
    this? I have been added if for a long time now and am pretty much stuck.
    One
    checkbox based on
    http://sltaylor.co.uk/blog/control-your-own-wordpress-custom-fields/ code
    works well, but my code two check several options to be stored does not.

    Jason @ WP Hacks mentioned this

    I may have missed something looking at your code, however the problem looks
    like the value you are using for your name attribute on the checkboxes.
    To store multiple values, your name has to be in the format of
    name="checkboxname[]" - the empty brackets let the processing script know
    that it is
    to accept an array of values, and not just an true/false value.

    So, try putting a set of square brackets after the name of your checkbox
    field and I'd assume you'll be good to go.

    I do not yet understand this yet unfortunately. maybe some extra feedback from the support people here will help.

    Thanks in advance for any input you guys can give me.

  2. Rhand
    Member
    Posted 4 years ago #

    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..

  3. Rhand
    Member
    Posted 4 years ago #

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

  4. Rhand
    Member
    Posted 4 years ago #

    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.

  5. Rhand
    Member
    Posted 4 years ago #

    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

  6. Rhand
    Member
    Posted 4 years ago #

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

  7. Rhand
    Member
    Posted 4 years ago #

    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?

  8. Rhand
    Member
    Posted 4 years ago #

    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;
  9. Rhand
    Member
    Posted 4 years ago #

    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;
    }
  10. Rhand
    Member
    Posted 4 years ago #

    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

  11. Rhand
    Member
    Posted 4 years ago #

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

  12. Rhand
    Member
    Posted 4 years ago #

    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.

  13. dorothai
    Member
    Posted 3 years ago #

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

    Thanks!!!!

Topic Closed

This topic has been closed to new replies.

About this Topic