admin管理员组文章数量:1244389
I'm sure this question has been asked many times, but I am struggling to get the basic premises of a checkbox to play nice with metaboxes. In detail, when I check the value of my checkbox and then click "update" on my page, I cannot see "the actual" checked value on POST. It does not matter if I physically check the box or not...I get no proper post value returned. It's always showing "on".
My Current Setup
- I'm adding a meta box
add_meta_box(
'_demo_meta_box_email',
'Demo - Email Manager',
array($this, 'render_meta_box_email_details'),
'demowebforms',
'normal',
'high',
array('foo' => 'bar')
);
- I then build my render function
public function render_meta_box_email_details($post) {
//define storage for keys on post
$getInputSelections = [];
if (isset($post->ID)) {
$getInputSelections = get_post_meta($post->ID);
//debug
//no change on Post, no matter if my below checkbox is checked or unchecked
$mcheckbox = get_post_meta($post->ID, '_email_Showvalidationcodeviaemail', false);
echo '<pre>' . var_export($mcheckbox, true) . '</pre>';
//its always showing me this on screen, no matter what...
//array (
//0 => 'on',
//)
}
$emailItems = [
"Show validation code via email" => "Yes",
//....other line items...
];
foreach ($emailItems as $key => $val) {
$keyname = str_replace(' ', '', $key);
if ($key == "Show validation code via email") {
echo '<input type="checkbox" id="email_' . $keyname . '" name="email_' . $keyname . '" aria-label="' . esc_attr($key) . '" >';
}
}
}
- I then attempt to save the post
public function save_demo_post_type_meta_data($post_id)
{
//WORDPRESS AUTOSAVING ENABLED? BAIL FROM THIS PROCESS
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
{
return;
}
//LETS CHECK AGAINST MULTIPLE PAREMETERS(KEYS) FOR OUR POSTS AND SAVE THE CONTENT
foreach ($_POST as $key => $val)
{
//WE ONLY CARE ABOUT THE EMAIL METABOX FOR NOW...
if ((strpos($key, 'email') === 0))
{
// MANAGE ALL CONTENT AND LIST POST ITEMS
if (isset($_POST[$key]))
{
//SERIALIZE
$mSerialize = maybe_serialize($_POST[$key]);
//SANITIZE
$mSanitize = sanitize_text_field($mSerialize);
//SAVE
update_post_meta($post_id, '_' . $key, $mSanitize);
}
return $post_id;
}
}
}
What am I missing, or doing wrong, to stop my checkbox from displaying a check or unchecked value on post?
Many many thanks
I'm sure this question has been asked many times, but I am struggling to get the basic premises of a checkbox to play nice with metaboxes. In detail, when I check the value of my checkbox and then click "update" on my page, I cannot see "the actual" checked value on POST. It does not matter if I physically check the box or not...I get no proper post value returned. It's always showing "on".
My Current Setup
- I'm adding a meta box
add_meta_box(
'_demo_meta_box_email',
'Demo - Email Manager',
array($this, 'render_meta_box_email_details'),
'demowebforms',
'normal',
'high',
array('foo' => 'bar')
);
- I then build my render function
public function render_meta_box_email_details($post) {
//define storage for keys on post
$getInputSelections = [];
if (isset($post->ID)) {
$getInputSelections = get_post_meta($post->ID);
//debug
//no change on Post, no matter if my below checkbox is checked or unchecked
$mcheckbox = get_post_meta($post->ID, '_email_Showvalidationcodeviaemail', false);
echo '<pre>' . var_export($mcheckbox, true) . '</pre>';
//its always showing me this on screen, no matter what...
//array (
//0 => 'on',
//)
}
$emailItems = [
"Show validation code via email" => "Yes",
//....other line items...
];
foreach ($emailItems as $key => $val) {
$keyname = str_replace(' ', '', $key);
if ($key == "Show validation code via email") {
echo '<input type="checkbox" id="email_' . $keyname . '" name="email_' . $keyname . '" aria-label="' . esc_attr($key) . '" >';
}
}
}
- I then attempt to save the post
public function save_demo_post_type_meta_data($post_id)
{
//WORDPRESS AUTOSAVING ENABLED? BAIL FROM THIS PROCESS
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
{
return;
}
//LETS CHECK AGAINST MULTIPLE PAREMETERS(KEYS) FOR OUR POSTS AND SAVE THE CONTENT
foreach ($_POST as $key => $val)
{
//WE ONLY CARE ABOUT THE EMAIL METABOX FOR NOW...
if ((strpos($key, 'email') === 0))
{
// MANAGE ALL CONTENT AND LIST POST ITEMS
if (isset($_POST[$key]))
{
//SERIALIZE
$mSerialize = maybe_serialize($_POST[$key]);
//SANITIZE
$mSanitize = sanitize_text_field($mSerialize);
//SAVE
update_post_meta($post_id, '_' . $key, $mSanitize);
}
return $post_id;
}
}
}
What am I missing, or doing wrong, to stop my checkbox from displaying a check or unchecked value on post?
Many many thanks
Share Improve this question edited Feb 13 at 19:51 klewis asked Feb 13 at 19:28 klewisklewis 8991 gold badge14 silver badges29 bronze badges 2 |1 Answer
Reset to default 2There are all kinds of code smells regarding why do you want to serialize and sanitize a check box value. The first is not needed, the second is useless when too many wrong values can be "true" as you seem to be ok with a value of "off" as well as "on"
But the main issue in your code is that you do not handle properly the case when the checkbox is not set. You assume that the request always includes you field when submitted but that is not true to checkboxes for which the request will include the field only when it "on" but not when its "off". Therefor your loop is wrong and you need to explicitly check for your checkbox related field, and clear the value from the meta when it is not set at all in the request. Therefor what is likely to have happened is that you tested that the "on" works and since then the value kept being "on" whatever you have done.
本文标签: How to get a checkbox element inside a metaboxto return its proper value on Post
版权声明:本文标题:How to get a checkbox element inside a metabox, to return its proper value on Post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740224149a2244370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$_POST
at the start ofsave_...
, it may help you to understand what appends. – mmm Commented Feb 13 at 19:42