0

I Just want to update (edit existing) the custom meta box variable from front-end. I got a form Front end post editing using a form & process things but in this code post-title i can edit & update, but i cannot edit & update values custom metabox variable values.

global $current_user;
wp_get_current_user();

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) )
{
$post_id   = $_POST['post_id'];
$post_type = 'testimonial';
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) )
{ 
    $post = array(
    'ID'             => esc_sql($post_id),
    'post_content'   => esc_sql($_POST['postcontent']),
    'post_title'     => esc_sql($_POST['post_title'])
);
    wp_update_post($post);

    if ( isset($_POST['package']) ) update_post_meta($post_id, 'zon_package', esc_sql($_POST['zon_package']) );
    if ( isset($_POST['phone']) ) update_post_meta($post_id, 'zon_phone', esc_sql($_POST['zon_phone']) );
                    }
                    else
                    {
                        wp_die("You can't do that");
                    }
                }

$args = array(
    'post_type' => 'testimonial',
    'post_status' => 'publish',
    'post_per_page' => 5,
    'author' => $current_user->ID,
    'meta_query' => array(
        array(
            'key' => '_zon_testimonial_key',
            'value' => 's:8:"approved";i:1;s:8:"featured";i:0;',
            'compare' => 'LIKE'
            )
    )
);
$query =new WP_Query($args);
if ($query->have_posts() || is_user_logged_in() || current_user_can('edit_post', $args->ID)) :
while ($query->have_posts()) : $query->the_post();
echo '<p>'.get_the_title().'</p>';
?>


<form class="royal_page" role="form" method="post">                 
        <input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
        <?php wp_nonce_field( 'zon_testimonial'.  get_the_ID(), 'zon_testimonial_nonce' );?>

        <p><label for="post_title">Title</label>
    <input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></p>

    <p><?php wp_editor( $post->post_content, 'postcontent' ); ?></p>

        <p><label for="post_title">Package</label>
        <?php $value = get_post_meta(get_the_ID(), '_zon_testimonial_key', true); ?>
        <input type="text" id="zon_package" name="zon_package" value="<?php echo esc_attr($value["package"]); ?>" /></p>

        <p><label for="post_title">Phone</label>
        <?php $value = get_post_meta(get_the_ID(), '_zon_testimonial_key', true); ?>
        <input type="text" id="zon_phone" name="zon_phone" value="<?php echo $value["phone"]; ?>" /></p>

    <input type="submit" id="submit" value="Update" />           
   </form>      

<?php endwhile; endif; ?>
0

That is easy, you are trying to update the wrong value.

Please replace the following

if ( isset($_POST['package']) ) update_post_meta($post_id, 'zon_package', esc_sql($_POST['zon_package']) );
if ( isset($_POST['zon_phone']) ) update_post_meta($post_id, 'phone', esc_sql($_POST['phone']) );

With this:

$zon_package = '';
$zon_phone = '';
if ( isset($_POST['package']) ) $zon_package = esc_sql($_POST['zon_package']);
if ( isset($_POST['zon_phone']) ) $zon_phone = esc_sql($_POST['phone']);
$zon_testimonial_key = array();
if(isset($zon_package) && isset($zon_phone)) {
    $zon_testimonial_key = array (
        'package' => $zon_package,
        'phone' => $zon_phone
    );
if(!empty($zon_testimonial_key)) {
    update_post_meta($post_id, '_zon_testimonial_key', $zon_testimonial_key );
}

You should be pushing the values as an array in _zon_testimonial_key key.

  • i don't clear How to make? – Noufal Binu Jul 14 at 3:22
  • I will edit my answer to be clearer. – Faham Shaikh Jul 14 at 4:00
  • How to set the value inside the form ? value="<?php echo $value["phone"]; ?> – Noufal Binu Jul 15 at 4:19
  • Please check the change that I have made to mu answer. – Faham Shaikh Jul 15 at 4:36
  • not working Undefined index: update_post_nonce in – Noufal Binu Jul 15 at 5:22

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.