metabox - Update Post Meta in Front End with a form

How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn

How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn't this work? Showing only old value not update, what i did wrong?

This is my Front-End Page, I'm using this code inside normal Page With shortCode and also this code between post loop.

if ( isset( $_POST['zon_testimonial_nonce'] ) && wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
    { //if nonce check succeeds.

        $post_id = $post->ID;
        $data = array(
            'package' => sanitize_text_field( $_POST['zon_package'] )
        );
        update_post_meta( $post_id, '_zon_testimonial_key', $data );
    }


$data = get_post_meta($post->ID, '_zon_testimonial_key', true);
$package = isset($data['package']) ? $data['package'] : '';

print_r($_POST);
?>


<form method="post" action="">
   <?php wp_nonce_field('zon_testimonial','zon_testimonial_nonce'); ?>
   <label>This is label</label>
   <input type='text' name='zon_package' value='<?php echo $package; ?>' />
   <input type='submit' value='save' />
</form>    

How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn't this work? Showing only old value not update, what i did wrong?

This is my Front-End Page, I'm using this code inside normal Page With shortCode and also this code between post loop.

if ( isset( $_POST['zon_testimonial_nonce'] ) && wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
    { //if nonce check succeeds.

        $post_id = $post->ID;
        $data = array(
            'package' => sanitize_text_field( $_POST['zon_package'] )
        );
        update_post_meta( $post_id, '_zon_testimonial_key', $data );
    }


$data = get_post_meta($post->ID, '_zon_testimonial_key', true);
$package = isset($data['package']) ? $data['package'] : '';

print_r($_POST);
?>


<form method="post" action="">
   <?php wp_nonce_field('zon_testimonial','zon_testimonial_nonce'); ?>
   <label>This is label</label>
   <input type='text' name='zon_package' value='<?php echo $package; ?>' />
   <input type='submit' value='save' />
</form>    
Share Improve this question edited Jul 23, 2019 at 9:40 Noufal Binu asked Jul 21, 2019 at 14:06 Noufal BinuNoufal Binu 2713 bronze badges 3
  • The whole code (update_post_meta() and displaying form) is in the same place / file? – nmr Commented Jul 22, 2019 at 6:52
  • @nmr same file. – Noufal Binu Commented Jul 22, 2019 at 14:05
  • Your shortcode works for me after adding global $post;, saves and reads the data correctly. Enable debugging in WP. Check value of post ID and $data before update_post_meta: echo "id={$post_id}, data= " . print_r($data, true); – nmr Commented Jul 23, 2019 at 7:49
Add a comment  | 

2 Answers 2

Reset to default 0

Since you are using this code in the shortcode, I suppose the reason is the lack of post ID.

You want to get the ID of the current post from the $post variable, but to make it possible you need to add the global $post; earlier (at the beginning of the function, or at least before the IF).

global $post;
if ( isset( $_POST['zon_testimonial_nonce'] ) && 
     wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )

You can also use the get_queried_object_id() function to get the ID of the current queried object.

$post_id = get_queried_object_id();
if ( isset( $_POST['zon_testimonial_nonce'] ) && 
     wp_verify_nonce($_POST['zon_testimonial_nonce'],'zon_testimonial') )
{
    $data = array(
        'package' => sanitize_text_field( $_POST['zon_package'] )
    );
    update_post_meta( $post_id, '_zon_testimonial_key', $data );
}
$data = get_post_meta($post_id, '_zon_testimonial_key', true);

Where do you put this script? The form will be submitted to index.php if you don't specify Form Action :

<form method="post" action="yourfile.php">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302946a4621548.html

相关推荐

  • metabox - Update Post Meta in Front End with a form

    How I can update Existing (Back-End) metabox variable from front-End, I tried many times but I don't know why doesn

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信