I'm trying to update user meta after admin updated the user data/user profile, but I can't get it work
Here my code :
<?php
add_action('show_user_profile', 'extra_user_profile_fields');
add_action('edit_user_profile', 'extra_user_profile_fields');
function extra_user_profile_fields($user)
{
$is_haloagen = esc_attr(get_user_meta($user->ID, 'is_haloagen', true));
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="is_haloagen"><?php _e("Apakah User Ini Member Haloagen?"); ?></label></th>
<td>
<select name="is_haloagen" id="is_haloagen">
<option value="no" <?= $is_haloagen == 'no' ? 'selected' : '' ?>>Tidak</option>
<option value="yes" <?= $is_haloagen == 'yes' ? 'selected' : '' ?>>Ya</option>
</select>
</td>
</tr>
</table>
<?php }
add_action('personal_options_update', 'save_extra_user_profile_fields');
add_action('edit_user_profile_update', 'save_extra_user_profile_fields');
function save_extra_user_profile_fields($user_id)
{
if (empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-user_' . $user_id)) {
return;
}
if (!current_user_can('edit_user', $user_id)) {
return false;
}
$is_haloagen = isset($_POST['is_haloagen']) ? sanitize_text_field($_POST['is_haloagen']) : '';
update_user_meta($user_id, 'is_haloagen', $is_haloagen); # << this is ok and the usermeta is updated
if ($is_haloagen === 'yes') {
# print_r('yes');exit; << if i open the comment, it print yes if $is_haloagen == 'yes', so it working right?
# below this, all usermeta is not being updated
update_user_meta($user_id, 'package_id', "2656079");
update_user_meta($user_id, 'package_listings', '-1');
update_user_meta($user_id, 'package_activation', date('Y-m-d H:i:s'));
update_user_meta($user_id, 'user_had_free_package', 'yes');
if ("2656079" != get_user_meta($user_id, 'package_id', true)) {
error_log("error tidak sama package id!");
wp_die(__('An error occurred', 'textdomain'));
}
}
return;
}
let me clear something, the usermeta with metakey is_haloagen is successfully updated but below it, is not being updated, I check the variable $is_haloagen, $user_id(just 1 user exist) and nothing coming to my mind why this piece of script is not working
i'm using wordpress 6.4.2 with php8.1
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand