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.

I followed the help provided in this question but cannot get the form save the data. I tried using 'save_post' action hook to try and save the post like we do for admin side. Any help would be highly appreciated.

My code is as below:

/* This is above the get_header() of the template */

if ( isset( $_POST['grn_data'] ) && wp_verify_nonce($_POST['grn_data'],'update_grn_postmeta') )
{ //if nonce check succeeds.
    global $post;
    $postid = $post->ID;
    $data = $_POST['grn'];
    update_post_meta($postid,'grn',$data);
}

And the form is in a seperate file and i am calling it whenever it is necessary.

<?php
/* This file is to provide input fields to record data from Front End */
function grn_data($c) {
        global $post;
        $grn = array();
?>
        <div class="grndata">
        <label for="grn" id="grnhead">GRN</label>
        <div class="toggle-fedata">
        <ul>
        <?php
        $curgrn = 'grn';
        $grnval = get_post_meta($post->ID, $curgrn, true); ?>
        <form method="post" action="">
        <?php wp_nonce_field('update_grn_data','grn_data'); ?>
        <input type="text" size="30" maxlength="30" name="<?php echo $curgrn; ?>" value="<?php echo $grnval; ?>" />
        <input type="submit" value="Save" />
        <?php var_dump($grnval); ?>
        <?php var_dump($curgrn); ?>
        </form>
        </div></div>
<?php }

?>

Please can someone advise on this? I have been stuck like this for days now.

share|improve this question
add comment

1 Answer

your nonce action name is wrong use same as entered in wp_nonce_field('update_grn_data','grn_data'); and also you forget to echo nonce, try this

if ( isset( $_POST['grn_data'] ) &&  wp_verify_nonce($_POST['grn_data'],'update_grn_data') ) {
    //if nonce check succeeds.
    $postid = $_POST['post_id'];
    $data = $_POST['grn'];
    update_post_meta($postid,'grn',$data);
}

form

<?php
/* This file is to provide input fields to record data from Front End */
function grn_data($post_id) { ?>
    <div class="grndata">
        <label for="grn" id="grnhead">GRN</label>
        <div class="toggle-fedata">
           <?php
           $curgrn = 'grn';
           $grnval = get_post_meta($post_id, $curgrn, true); ?>
           <form method="post" action="">
              <?php echo wp_nonce_field('update_grn_data','grn_data'); ?>
              <input type="text" size="30" maxlength="30" name="<?php echo $curgrn; ?>" value="<?php echo $grnval; ?>" />
              <input type="submit" value="Save" />
              <?php var_dump($grnval); ?>
              <?php var_dump($curgrn); ?>
          </form>
      </div>
   </div>
<?php } ?>

now call the form

echo grn_data($post->ID);
share|improve this answer
    
Thanks for your answer but i still see a null value for $grnval. Not getting what went wrong. –  sri Aug 19 '13 at 9:41
    
try to pass $post->ID in grn_data($post->ID) function and add a hidden field to form for post_id i am updating the code. –  Anjum Aug 19 '13 at 10:12
    
The problem appears to be something different. I am querying another post type inside a different post type. So the conflict is with the post id's of different types. I now have to figure out a way to solve it. Thanks for your help though. –  sri Aug 19 '13 at 10:36
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.