How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn't this work? Showing only old value not update, what i did wrong?
This is my Front-End Page, I'm using this code inside normal Page With shortCode and also this code between post loop.
if ( isset( $_POST['zon_testimonial_nonce'] ) && wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
{ //if nonce check succeeds.
$post_id = $post->ID;
$data = array(
'package' => sanitize_text_field( $_POST['zon_package'] )
);
update_post_meta( $post_id, '_zon_testimonial_key', $data );
}
$data = get_post_meta($post->ID, '_zon_testimonial_key', true);
$package = isset($data['package']) ? $data['package'] : '';
print_r($_POST);
?>
<form method="post" action="">
<?php wp_nonce_field('zon_testimonial','zon_testimonial_nonce'); ?>
<label>This is label</label>
<input type='text' name='zon_package' value='<?php echo $package; ?>' />
<input type='submit' value='save' />
</form>
How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn't this work? Showing only old value not update, what i did wrong?
This is my Front-End Page, I'm using this code inside normal Page With shortCode and also this code between post loop.
if ( isset( $_POST['zon_testimonial_nonce'] ) && wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
{ //if nonce check succeeds.
$post_id = $post->ID;
$data = array(
'package' => sanitize_text_field( $_POST['zon_package'] )
);
update_post_meta( $post_id, '_zon_testimonial_key', $data );
}
$data = get_post_meta($post->ID, '_zon_testimonial_key', true);
$package = isset($data['package']) ? $data['package'] : '';
print_r($_POST);
?>
<form method="post" action="">
<?php wp_nonce_field('zon_testimonial','zon_testimonial_nonce'); ?>
<label>This is label</label>
<input type='text' name='zon_package' value='<?php echo $package; ?>' />
<input type='submit' value='save' />
</form>
Share
Improve this question
edited Jul 23, 2019 at 9:40
Noufal Binu
asked Jul 21, 2019 at 14:06
Noufal BinuNoufal Binu
2713 bronze badges
3
|
2 Answers
Reset to default 0Since you are using this code in the shortcode, I suppose the reason is the lack of post ID.
You want to get the ID of the current post from the $post
variable, but to make it possible you need to add the global $post;
earlier (at the beginning of the function, or at least before the IF
).
global $post;
if ( isset( $_POST['zon_testimonial_nonce'] ) &&
wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
You can also use the get_queried_object_id()
function to get the ID of the current queried object.
$post_id = get_queried_object_id();
if ( isset( $_POST['zon_testimonial_nonce'] ) &&
wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
{
$data = array(
'package' => sanitize_text_field( $_POST['zon_package'] )
);
update_post_meta( $post_id, '_zon_testimonial_key', $data );
}
$data = get_post_meta($post_id, '_zon_testimonial_key', true);
Where do you put this script? The form will be submitted to index.php
if you don't specify Form Action :
<form method="post" action="yourfile.php">
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302946a4621548.html
update_post_meta()
and displaying form) is in the same place / file? – nmr Commented Jul 22, 2019 at 6:52global $post;
, saves and reads the data correctly. Enable debugging in WP. Check value of post ID and$data
beforeupdate_post_meta
:echo "id={$post_id}, data= " . print_r($data, true);
– nmr Commented Jul 23, 2019 at 7:49