Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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>
share|improve this answer
    
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') ) –  mattrepublic Jun 29 '11 at 20:24
    
my bad, fixed it. –  Hameedullah Khan Jun 29 '11 at 20:31
    
No worries, thank you for your help! –  mattrepublic Jun 30 '11 at 0:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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