I’m working on a WordPress project where I need to update a user meta value from the frontend. The problem is that the stored value in the database doesn’t match what I input. Here’s what’s happening:
I’ve set up a form using a shortcode that takes user input. When submitted, it should update a meta value for the current user. The input I’m trying to store looks like this:
a:1:{i:0;s:1:"1";}
But when I check the database, it’s stored as:
s:1"a:1:{i:0;s:1:"8";}"
I’ve tried sanitizing the input and even deleting the meta key before updating, but no luck. Any ideas on how to make sure the stored value is exactly the same as what’s entered in the form?
Here’s a simplified version of my code:
function my_custom_form() {
$output = '<form method="post">';
$output .= '<input type="text" name="user_input">';
$output .= '<input type="submit" name="submit_form">';
$output .= '</form>';
return $output;
}
add_shortcode('custom_form', 'my_custom_form');
function handle_form_submission() {
if (isset($_POST['submit_form'])) {
$input = sanitize_text_field($_POST['user_input']);
$user_id = get_current_user_id();
update_user_meta($user_id, 'custom_meta_key', $input);
}
}
add_action('init', 'handle_form_submission');
What am I missing here? How can I make sure the meta value is stored exactly as it appears on the frontend?