add post meta front end edit

I’m trying to allow users to add meta values in my posts but it doesn’t seem to be adding it in my front end post edit

<!-- edit Post Form -->
<div id="postbox">
<form id="edit_post" name="edit_post" method="post" action="">
</p>
<p><label for="description">Description</label><br />
<textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
</p>
<!-- wine Rating -->
<fieldset class="data">
          <label for="data">data</label>
          <input type="text" value="" id="data" size="60" tabindex="20" name="data"></textarea>
</fieldset>

<p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="edit_post" />
<input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
<?php wp_nonce_field( 'edit-post' ); ?>
</form>
</div>
<?php

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "edit_post" && isset($_POST['pid'])) {
$the_post = get_post($_POST['pid']); 


$the_post = array(); 
$the_post['post_content'] = $_POST['description'];
$the_post['data'] = array($_POST['data']);

add_post_meta($pid, 'rating', $the_post['data'], true);

  $pid = wp_update_post($the_post); 


  $link = get_permalink( $pid );
  wp_redirect($link);
}
?>

but I can’t seem to get it to work for the front end editing I’m pretty sure I’m doing it wrong help…

Solutions Collecting From Web of "add post meta front end edit"

This code should be conditional first and then form output since you can’t use wp_redirect after headers are set and you are updating so use update_post_meta instead of add_post_meta.

Try:

    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "edit_post" && isset($_POST['pid'])) {
    $the_post = get_post($_POST['pid']); 


    $the_post = array(); 
    $the_post['post_content'] = $_POST['description'];
    $the_post['data'] = array($_POST['data']);

    $pid = wp_update_post($the_post); 
    update_post_meta($pid, 'rating', $the_post['data'], true);

    $link = get_permalink( $pid );
    wp_redirect($link);
}

<!-- edit Post Form -->
    <div id="postbox">
        <form id="edit_post" name="edit_post" method="post" action="">
        <p><label for="description">Description</label><br />
            <textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
        </p>
        <!-- wine Rating -->
        <fieldset class="data">
                  <label for="data">data</label>
                  <input type="text" value="" id="data" size="60" tabindex="20" name="data"><?php echo (!empty($val = get_post_meta($post_to_edit->ID,'rating',true))) ? $val : ''; ?></textarea>
        </fieldset>

        <p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
        <input type="hidden" name="action" value="edit_post" />
        <input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
        <?php wp_nonce_field( 'edit-post' ); ?>
        </form>
    </div>