Thanks to @Anu’s correction I now have a function to update_post_meta from the front end.
Something is wrong in my script however, because with the function below nothing is being displayed on the page. It only displays is a white screen. if I take out the if ( empty($_POST) || section the page template will display, but any changes I make to the form are not saving.
What am I missing?
// top of page
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
{ //if fail nonce check, exit script
exit;
}
else :
{
global $post;
$postid = $post->ID;
$data = $_POST['priceone'];
update_post_meta($postid,'metakey',$data);
}
endif;
// in single.php
$priceone = get_post_meta($post->ID, 'priceone', true);
<form method="post" action="">
<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
<label>This is label</label>
<input type='text' name='priceone' value='<?php echo $priceone ?>' />
<input type='submit' value='save' />
</form>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Change your nonce verify to actually verify and update post meta, else do nothing. But don’t use exit at the top of your template.
// top of page
if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') )
{ //if nonce check succeeds.
global $post;
$postid = $post->ID;
$data = $_POST['priceone'];
update_post_meta($postid,'metakey',$data);
}
// in single.php
$priceone = get_post_meta($post->ID, 'priceone', true);
<form method="post" action="">
<?php wp_nonce_field('update_drw_postmeta','drw_inventory'); ?>
<label>This is label</label>
<input type='text' name='priceone' value='<?php echo $priceone ?>' />
<input type='submit' value='save' />
</form>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0