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.
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> ';
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
Using
case "checkboxes": {
// Checkboxes Technology
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label> ';
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> ';
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> ';
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> ';
$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.
dorothai
Member
Posted 1 year ago #
no checked here spent days to get the echo checked -- so if you have the code can you please post it?
Thanks!!!!