• Resolved Rhand

    (@rhand)


    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 @
    https://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
    https://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.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter Rhand

    (@rhand)

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

    Thread Starter Rhand

    (@rhand)

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

    Thread Starter Rhand

    (@rhand)

    latest code in which the multiple check boxes do not work is here: https://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.

    Thread Starter Rhand

    (@rhand)

    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 https://pastebin.com/txtLUHqC

    Thread Starter Rhand

    (@rhand)

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

    Thread Starter Rhand

    (@rhand)

    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?

    Thread Starter Rhand

    (@rhand)

    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;

    Thread Starter Rhand

    (@rhand)

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

    Thread Starter Rhand

    (@rhand)

    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: https://pastebin.com/yGaUFZZM

    Thread Starter Rhand

    (@rhand)

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

    Thread Starter Rhand

    (@rhand)

    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 ‘Custom field multiple checkboxes’ is closed to new replies.