There's something wrong with how I'm managing my checkbox with in my HTML input element - as in the database, the value stays at 1, and the form always shows 1, no matter if it's un-ticked.
Any idea please?
I've googled a lot but I'm also struggling to understand the concept of how the checkbox works with the setting of (in HTML) and storing of (in PHP), as most of the results are for post_meta, but this is for user_meta key / value.
<?php
// set the HTML for the inputs
function show_extra_profile_fields( $user ) {
$is_user_archived = get_user_meta( $user->ID, 'archived_user', true );
?>
<table class="form-table">
<tr>
<th><label for="archived_user"><?php esc_html_e( 'Archive user', 'xlearn' ); ?></label></th>
<td>
<input type="checkbox"
id="archived_user"
name="archived_user"
class="checkbox"
value="yes"
<?php if (get_user_meta( $user->ID, 'archived_user', true ) == "1") echo "checked" ;?>
/>
</td>
</tr>
</table>
<?php
}
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
// update the user meta on save
function update_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if ( isset( $_POST['archived_user'] ) ) {
update_user_meta( $user_id, 'archived_user', $_POST['archived_user'] );
}
}
add_action( 'edit_user_profile_update', 'update_profile_fields' );