I tried updating a meta value from wordpress frontend using the following code:
function code_form()
{
$content = '';
$content .= '<h2> code </h2>';
$content .='<form method="post">';
$content .='<input type = "text" name="code"/>';
$content .= '<br /><input type="submit" name="Activate" value="continue">';
$content .= '</form>';
return $content;
}
add_shortcode('form1','code_form');
function a()
{
if(isset($_POST['Activate']))
{
$input = sanitize_text_field($_POST['code']);
$current_user_id = get_current_user_id();
update_user_meta($current_user_id, 'metakey_' ,$input);
echo "<script>alert('success')</script>";
}
}
add_action('wp_head','a');
And the input value should be something like:
a:1:{i:0;s:1:"1";}
However, when I check phpmyadmin for the updated result, something different is stored, such as
s:1"a:1:{i:0;s:1:"8";}"
How should I change my code to make the stored value exactly same as my input?
I tried deleting the metakey and update the value, but still doesn't work.
CodePudding user response:
Your input data is serialized twice that's why it happens, try to pass an array
$input = explode(',' , $input);