I'm trying to update user meta but is always 1, What I doing wrong? [on hold]












1














<?php function consultation_user_profile_fields($user){
if ( user_can( $user->ID, "subscriber" ) ) {
?>
<h3>Consultation</h3>
<?php $query = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
if ($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
$hasAccess = '';
if ( is_object( $user ) && isset( $user->ID ) ) {

$hasAccess = get_user_meta(
$user>ID,'consultation'.get_the_ID(), true );

} ?>
<div class="consultation">
<span>Has access&nbsp;</span>
<div class="title"><?php the_title(); ?></div>
<input type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>/>
</div>
<?php endwhile; endif;
} else {
return;
}
}
add_action( 'show_user_profile', 'consultation_user_profile_fields' );
add_action( 'edit_user_profile', 'consultation_user_profile_fields' );
add_action( "user_new_form", "consultation_user_profile_fields" );
function save_consultation_user_profile_fields($user_id){
if(!current_user_can('manage_options'))
return false;

foreach ($_POST['consultation'] as $key => $val) {
update_user_meta($user_id,'consultation'.$key,$_POST['consultation'][$key]);
}
# save my custom field

}
add_action( 'user_register', 'save_consultation_user_profile_fields');
add_action('personal_options_update','save_consultation_user_profile_fields' );
add_action( 'edit_user_profile_update','save_consultation_user_profile_fields' );









share|improve this question







New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Jacob Peattie, fuxia yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – fuxia

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
    – Jacob Peattie
    yesterday










  • And what value are you expecting? All your checkboxes has value set as 1, so...
    – Krzysiek Dróżdż
    yesterday










  • so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
    – pawel1993
    yesterday
















1














<?php function consultation_user_profile_fields($user){
if ( user_can( $user->ID, "subscriber" ) ) {
?>
<h3>Consultation</h3>
<?php $query = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
if ($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
$hasAccess = '';
if ( is_object( $user ) && isset( $user->ID ) ) {

$hasAccess = get_user_meta(
$user>ID,'consultation'.get_the_ID(), true );

} ?>
<div class="consultation">
<span>Has access&nbsp;</span>
<div class="title"><?php the_title(); ?></div>
<input type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>/>
</div>
<?php endwhile; endif;
} else {
return;
}
}
add_action( 'show_user_profile', 'consultation_user_profile_fields' );
add_action( 'edit_user_profile', 'consultation_user_profile_fields' );
add_action( "user_new_form", "consultation_user_profile_fields" );
function save_consultation_user_profile_fields($user_id){
if(!current_user_can('manage_options'))
return false;

foreach ($_POST['consultation'] as $key => $val) {
update_user_meta($user_id,'consultation'.$key,$_POST['consultation'][$key]);
}
# save my custom field

}
add_action( 'user_register', 'save_consultation_user_profile_fields');
add_action('personal_options_update','save_consultation_user_profile_fields' );
add_action( 'edit_user_profile_update','save_consultation_user_profile_fields' );









share|improve this question







New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Jacob Peattie, fuxia yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – fuxia

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
    – Jacob Peattie
    yesterday










  • And what value are you expecting? All your checkboxes has value set as 1, so...
    – Krzysiek Dróżdż
    yesterday










  • so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
    – pawel1993
    yesterday














1












1








1







<?php function consultation_user_profile_fields($user){
if ( user_can( $user->ID, "subscriber" ) ) {
?>
<h3>Consultation</h3>
<?php $query = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
if ($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
$hasAccess = '';
if ( is_object( $user ) && isset( $user->ID ) ) {

$hasAccess = get_user_meta(
$user>ID,'consultation'.get_the_ID(), true );

} ?>
<div class="consultation">
<span>Has access&nbsp;</span>
<div class="title"><?php the_title(); ?></div>
<input type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>/>
</div>
<?php endwhile; endif;
} else {
return;
}
}
add_action( 'show_user_profile', 'consultation_user_profile_fields' );
add_action( 'edit_user_profile', 'consultation_user_profile_fields' );
add_action( "user_new_form", "consultation_user_profile_fields" );
function save_consultation_user_profile_fields($user_id){
if(!current_user_can('manage_options'))
return false;

foreach ($_POST['consultation'] as $key => $val) {
update_user_meta($user_id,'consultation'.$key,$_POST['consultation'][$key]);
}
# save my custom field

}
add_action( 'user_register', 'save_consultation_user_profile_fields');
add_action('personal_options_update','save_consultation_user_profile_fields' );
add_action( 'edit_user_profile_update','save_consultation_user_profile_fields' );









share|improve this question







New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











<?php function consultation_user_profile_fields($user){
if ( user_can( $user->ID, "subscriber" ) ) {
?>
<h3>Consultation</h3>
<?php $query = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
if ($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
$hasAccess = '';
if ( is_object( $user ) && isset( $user->ID ) ) {

$hasAccess = get_user_meta(
$user>ID,'consultation'.get_the_ID(), true );

} ?>
<div class="consultation">
<span>Has access&nbsp;</span>
<div class="title"><?php the_title(); ?></div>
<input type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>/>
</div>
<?php endwhile; endif;
} else {
return;
}
}
add_action( 'show_user_profile', 'consultation_user_profile_fields' );
add_action( 'edit_user_profile', 'consultation_user_profile_fields' );
add_action( "user_new_form", "consultation_user_profile_fields" );
function save_consultation_user_profile_fields($user_id){
if(!current_user_can('manage_options'))
return false;

foreach ($_POST['consultation'] as $key => $val) {
update_user_meta($user_id,'consultation'.$key,$_POST['consultation'][$key]);
}
# save my custom field

}
add_action( 'user_register', 'save_consultation_user_profile_fields');
add_action('personal_options_update','save_consultation_user_profile_fields' );
add_action( 'edit_user_profile_update','save_consultation_user_profile_fields' );






plugins user-meta






share|improve this question







New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









pawel1993pawel1993

82




82




New contributor




pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






pawel1993 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Jacob Peattie, fuxia yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – fuxia

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Jacob Peattie, fuxia yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – fuxia

If this question can be reworded to fit the rules in the help center, please edit the question.












  • Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
    – Jacob Peattie
    yesterday










  • And what value are you expecting? All your checkboxes has value set as 1, so...
    – Krzysiek Dróżdż
    yesterday










  • so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
    – pawel1993
    yesterday


















  • Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
    – Jacob Peattie
    yesterday










  • And what value are you expecting? All your checkboxes has value set as 1, so...
    – Krzysiek Dróżdż
    yesterday










  • so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
    – pawel1993
    yesterday
















Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
– Jacob Peattie
yesterday




Probably not the actual problem, but you're missing a character here: $user>ID. Needs to be $user->ID.
– Jacob Peattie
yesterday












And what value are you expecting? All your checkboxes has value set as 1, so...
– Krzysiek Dróżdż
yesterday




And what value are you expecting? All your checkboxes has value set as 1, so...
– Krzysiek Dróżdż
yesterday












so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
– pawel1993
yesterday




so value="<?= ($hasAccess == 1 ? 1 : 0);?>" ? this is nothing changes
– pawel1993
yesterday










2 Answers
2






active

oldest

votes


















1














Your user_meta is not updated, because you process checkboxes incorrectly...



First of all... You've set values of all your checkboxes to 1. So if they're checked, then their value is 1 - and this is the value you save as user_meta.



On the other hand, when the checkbox is not checked, then it won't be sent in POST request. So your foreach loop won't loop through them - so the user_meta will never be set to anything other than 1 ;)



How to fix it?



You shouldn't iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).



Also, you should not trust user inputs so much - what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ;)



So I would do it like so:



function save_consultation_user_profile_fields( $user_id ) {
# save my custom field
if( ! current_user_can('manage_options') )
return false;

$query = new WP_Query( // it's the same query you use to create checkboxes in form
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
foreach ( $query->posts as $post ) {
if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
} else {
delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
}
}
}


This way you'll be sure, that users can gain access only for existing posts. You will be nicer for DB also - if user has no access to post, then no meta is stored in DB.






share|improve this answer























  • Krzysiek could you take my small this snippet?
    – pawel1993
    yesterday










  • @pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
    – Krzysiek Dróżdż
    yesterday



















1














The problem is that when the checkbox is not checked, $_POST['consultation'][$key] does not exist for that key. No value is posted for an unchecked checkbox.



So $_POST['consultation'] will only contain values for the checked checkboxes. This means that the existing saved values for the other checkboxes are not changed.



The simplest way to post a value for an unchecked checkbox is to have a hidden input before it with the same name and a different value. Like this:



<input type="hidden" name="consultation[<?= get_the_ID(); ?>]" value="0">
<input
type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>
/>


Or with less repeating yourself:



printf(
'<input id="%1$s" name="%1$s" type="hidden" value="0">
<input name="%1$s" type="checkbox" value="1" %2$s>',
esc_attr( 'consultation[' . get_the_ID() . ']' ),
checked( '1', $hasAccess, false )
);


So now if the checkbox is unchecked, $_POST['consultation'][$key] will be the value of the hidden input, 0.






share|improve this answer





















  • Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
    – Krzysiek Dróżdż
    yesterday










  • Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
    – Jacob Peattie
    yesterday










  • Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
    – Krzysiek Dróżdż
    yesterday


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your user_meta is not updated, because you process checkboxes incorrectly...



First of all... You've set values of all your checkboxes to 1. So if they're checked, then their value is 1 - and this is the value you save as user_meta.



On the other hand, when the checkbox is not checked, then it won't be sent in POST request. So your foreach loop won't loop through them - so the user_meta will never be set to anything other than 1 ;)



How to fix it?



You shouldn't iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).



Also, you should not trust user inputs so much - what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ;)



So I would do it like so:



function save_consultation_user_profile_fields( $user_id ) {
# save my custom field
if( ! current_user_can('manage_options') )
return false;

$query = new WP_Query( // it's the same query you use to create checkboxes in form
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
foreach ( $query->posts as $post ) {
if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
} else {
delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
}
}
}


This way you'll be sure, that users can gain access only for existing posts. You will be nicer for DB also - if user has no access to post, then no meta is stored in DB.






share|improve this answer























  • Krzysiek could you take my small this snippet?
    – pawel1993
    yesterday










  • @pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
    – Krzysiek Dróżdż
    yesterday
















1














Your user_meta is not updated, because you process checkboxes incorrectly...



First of all... You've set values of all your checkboxes to 1. So if they're checked, then their value is 1 - and this is the value you save as user_meta.



On the other hand, when the checkbox is not checked, then it won't be sent in POST request. So your foreach loop won't loop through them - so the user_meta will never be set to anything other than 1 ;)



How to fix it?



You shouldn't iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).



Also, you should not trust user inputs so much - what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ;)



So I would do it like so:



function save_consultation_user_profile_fields( $user_id ) {
# save my custom field
if( ! current_user_can('manage_options') )
return false;

$query = new WP_Query( // it's the same query you use to create checkboxes in form
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
foreach ( $query->posts as $post ) {
if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
} else {
delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
}
}
}


This way you'll be sure, that users can gain access only for existing posts. You will be nicer for DB also - if user has no access to post, then no meta is stored in DB.






share|improve this answer























  • Krzysiek could you take my small this snippet?
    – pawel1993
    yesterday










  • @pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
    – Krzysiek Dróżdż
    yesterday














1












1








1






Your user_meta is not updated, because you process checkboxes incorrectly...



First of all... You've set values of all your checkboxes to 1. So if they're checked, then their value is 1 - and this is the value you save as user_meta.



On the other hand, when the checkbox is not checked, then it won't be sent in POST request. So your foreach loop won't loop through them - so the user_meta will never be set to anything other than 1 ;)



How to fix it?



You shouldn't iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).



Also, you should not trust user inputs so much - what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ;)



So I would do it like so:



function save_consultation_user_profile_fields( $user_id ) {
# save my custom field
if( ! current_user_can('manage_options') )
return false;

$query = new WP_Query( // it's the same query you use to create checkboxes in form
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
foreach ( $query->posts as $post ) {
if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
} else {
delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
}
}
}


This way you'll be sure, that users can gain access only for existing posts. You will be nicer for DB also - if user has no access to post, then no meta is stored in DB.






share|improve this answer














Your user_meta is not updated, because you process checkboxes incorrectly...



First of all... You've set values of all your checkboxes to 1. So if they're checked, then their value is 1 - and this is the value you save as user_meta.



On the other hand, when the checkbox is not checked, then it won't be sent in POST request. So your foreach loop won't loop through them - so the user_meta will never be set to anything other than 1 ;)



How to fix it?



You shouldn't iterate posted array and loop through posts instead. And inside that loop you should check if given value is set in POST request and process it properly (set user_meta to 1, if the value is set, and to 0 if the value is not set).



Also, you should not trust user inputs so much - what if user tampers with data and makes some changes to IDs in checkboxes? He will get access to other posts ;)



So I would do it like so:



function save_consultation_user_profile_fields( $user_id ) {
# save my custom field
if( ! current_user_can('manage_options') )
return false;

$query = new WP_Query( // it's the same query you use to create checkboxes in form
array(
'posts_per_page' => -1,
'post_type' => 'consultation',
'post_status' => 'publish'
)
);
foreach ( $query->posts as $post ) {
if ( isset( $_POST['consultation'][$post->ID] ) && 1 == $_POST['consultation'][$post->ID] ) {
update_user_meta( $user_id, 'consultation' . $post->ID, 1 );
} else {
delete_user_meta( $user_id, 'consultation' . $post->ID, 1 );
}
}
}


This way you'll be sure, that users can gain access only for existing posts. You will be nicer for DB also - if user has no access to post, then no meta is stored in DB.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Krzysiek DróżdżKrzysiek Dróżdż

14.2k52741




14.2k52741












  • Krzysiek could you take my small this snippet?
    – pawel1993
    yesterday










  • @pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
    – Krzysiek Dróżdż
    yesterday


















  • Krzysiek could you take my small this snippet?
    – pawel1993
    yesterday










  • @pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
    – Krzysiek Dróżdż
    yesterday
















Krzysiek could you take my small this snippet?
– pawel1993
yesterday




Krzysiek could you take my small this snippet?
– pawel1993
yesterday












@pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
– Krzysiek Dróżdż
yesterday




@pawel1993 I've updated my answer with some code and explanation, why this approach is better and safer.
– Krzysiek Dróżdż
yesterday













1














The problem is that when the checkbox is not checked, $_POST['consultation'][$key] does not exist for that key. No value is posted for an unchecked checkbox.



So $_POST['consultation'] will only contain values for the checked checkboxes. This means that the existing saved values for the other checkboxes are not changed.



The simplest way to post a value for an unchecked checkbox is to have a hidden input before it with the same name and a different value. Like this:



<input type="hidden" name="consultation[<?= get_the_ID(); ?>]" value="0">
<input
type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>
/>


Or with less repeating yourself:



printf(
'<input id="%1$s" name="%1$s" type="hidden" value="0">
<input name="%1$s" type="checkbox" value="1" %2$s>',
esc_attr( 'consultation[' . get_the_ID() . ']' ),
checked( '1', $hasAccess, false )
);


So now if the checkbox is unchecked, $_POST['consultation'][$key] will be the value of the hidden input, 0.






share|improve this answer





















  • Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
    – Krzysiek Dróżdż
    yesterday










  • Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
    – Jacob Peattie
    yesterday










  • Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
    – Krzysiek Dróżdż
    yesterday
















1














The problem is that when the checkbox is not checked, $_POST['consultation'][$key] does not exist for that key. No value is posted for an unchecked checkbox.



So $_POST['consultation'] will only contain values for the checked checkboxes. This means that the existing saved values for the other checkboxes are not changed.



The simplest way to post a value for an unchecked checkbox is to have a hidden input before it with the same name and a different value. Like this:



<input type="hidden" name="consultation[<?= get_the_ID(); ?>]" value="0">
<input
type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>
/>


Or with less repeating yourself:



printf(
'<input id="%1$s" name="%1$s" type="hidden" value="0">
<input name="%1$s" type="checkbox" value="1" %2$s>',
esc_attr( 'consultation[' . get_the_ID() . ']' ),
checked( '1', $hasAccess, false )
);


So now if the checkbox is unchecked, $_POST['consultation'][$key] will be the value of the hidden input, 0.






share|improve this answer





















  • Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
    – Krzysiek Dróżdż
    yesterday










  • Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
    – Jacob Peattie
    yesterday










  • Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
    – Krzysiek Dróżdż
    yesterday














1












1








1






The problem is that when the checkbox is not checked, $_POST['consultation'][$key] does not exist for that key. No value is posted for an unchecked checkbox.



So $_POST['consultation'] will only contain values for the checked checkboxes. This means that the existing saved values for the other checkboxes are not changed.



The simplest way to post a value for an unchecked checkbox is to have a hidden input before it with the same name and a different value. Like this:



<input type="hidden" name="consultation[<?= get_the_ID(); ?>]" value="0">
<input
type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>
/>


Or with less repeating yourself:



printf(
'<input id="%1$s" name="%1$s" type="hidden" value="0">
<input name="%1$s" type="checkbox" value="1" %2$s>',
esc_attr( 'consultation[' . get_the_ID() . ']' ),
checked( '1', $hasAccess, false )
);


So now if the checkbox is unchecked, $_POST['consultation'][$key] will be the value of the hidden input, 0.






share|improve this answer












The problem is that when the checkbox is not checked, $_POST['consultation'][$key] does not exist for that key. No value is posted for an unchecked checkbox.



So $_POST['consultation'] will only contain values for the checked checkboxes. This means that the existing saved values for the other checkboxes are not changed.



The simplest way to post a value for an unchecked checkbox is to have a hidden input before it with the same name and a different value. Like this:



<input type="hidden" name="consultation[<?= get_the_ID(); ?>]" value="0">
<input
type="checkbox" id="consultation[<?= get_the_ID(); ?>]"
class="regular-text" name="consultation[<?= get_the_ID(); ?>]"
value="1" <?= ($hasAccess == 1 ? 'checked' : ''); ?>
/>


Or with less repeating yourself:



printf(
'<input id="%1$s" name="%1$s" type="hidden" value="0">
<input name="%1$s" type="checkbox" value="1" %2$s>',
esc_attr( 'consultation[' . get_the_ID() . ']' ),
checked( '1', $hasAccess, false )
);


So now if the checkbox is unchecked, $_POST['consultation'][$key] will be the value of the hidden input, 0.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Jacob PeattieJacob Peattie

15.3k41927




15.3k41927












  • Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
    – Krzysiek Dróżdż
    yesterday










  • Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
    – Jacob Peattie
    yesterday










  • Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
    – Krzysiek Dróżdż
    yesterday


















  • Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
    – Krzysiek Dróżdż
    yesterday










  • Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
    – Jacob Peattie
    yesterday










  • Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
    – Krzysiek Dróżdż
    yesterday
















Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
– Krzysiek Dróżdż
yesterday




Nice trick. On the other hand, I can gain access to all posts, if I prepare the POST request - there is still no validation in saving function.
– Krzysiek Dróżdż
yesterday












Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
– Jacob Peattie
yesterday




Right, but this isn’t code review. The question was about values for unchecked checkboxes, so that’s what I’ve addressed.
– Jacob Peattie
yesterday












Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
– Krzysiek Dróżdż
yesterday




Well, you're right, but... It's always good idea to promote best practices and comment on such bad/unsafe solutions in posted code. There's no harm in that, I guess...
– Krzysiek Dróżdż
yesterday



Popular posts from this blog

1300-talet

1300-talet

Has there ever been an instance of an active nuclear power plant within or near a war zone?