0

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>
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>
  • the if syntax is missing a ) or a , ... Does this look correct to you? if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_drw_postmeta') ) – m-torin Jun 29 '11 at 20:24
  • my bad, fixed it. – Hameedullah Khan Jun 29 '11 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.