0

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>    
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. 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

2 Answers 2

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);
2
  • i think problem in my nonce check. Commented Jul 23, 2019 at 3:44
  • already did global scope top my code. Commented Jul 23, 2019 at 5:31
0

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">
8
  • You can pass an array to a update_post_meta() and the array will be serialized into a string
    – nmr
    Commented Jul 22, 2019 at 6:43
  • @nmr thank you! My mistake. I will update the answer
    – Jamy
    Commented Jul 22, 2019 at 9:27
  • @Jamy same place Commented Jul 22, 2019 at 14:04
  • 1
    Inside a plugin and called with a short code inside wordpress page. Commented Jul 22, 2019 at 16:42
  • 1
    @NoufalBinu you should debug your code. Turn on display_errors setting, var_dump $post and the return value of update_post_meta() function. Make sure the conditions in the if statement return true, etc
    – Jamy
    Commented Jul 23, 2019 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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