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>    
  • The whole code (update_post_meta() and displaying form) is in the same place / file? – nmr Jul 22 at 6:52
  • @nmr same file. – Noufal Binu Jul 22 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 Jul 23 at 7:49
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);
  • i think problem in my nonce check. – Noufal Binu Jul 23 at 3:44
  • already did global scope top my code. – Noufal Binu Jul 23 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">
  • You can pass an array to a update_post_meta() and the array will be serialized into a string – nmr Jul 22 at 6:43
  • @nmr thank you! My mistake. I will update the answer – Jamy Jul 22 at 9:27
  • @Jamy same place – Noufal Binu Jul 22 at 14:04
  • 1
    Inside a plugin and called with a short code inside wordpress page. – Noufal Binu Jul 22 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 Jul 23 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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