0

My custom post type have a meta box that is listing another custom post types in its meta box. And it is causing error each time I try to publish a post with that custom post type. It is always bring to a page that say "Are You Sure You Want To Do This? Please Try Again". It does not have any problem in my localhost, but does not work when I run it on my web hosting.

Here is the codes:

add_action( 'add_meta_boxes', 'ind_pricing_tables_boxes' );

function ind_pricing_tables_boxes() {
    $screens = array( 'indtable' );
    foreach ($screens as $screen) {
    add_meta_box('indtable_boxid', __( 'Pricing Table', 'pricing_table' ), 'ind_pricing_tables_boxes_form', $screen );
    }
}

function ind_pricing_tables_boxes_form() {
    global $post;
        wp_nonce_field( 'ind_pricing_table_box_nonce' );
    $ind_table_packs = get_post_meta($post->ID, "_ind_meta_table_keypacks", true);
    $ind_table_packs = ($ind_table_packs == '') ? array() : json_decode($ind_table_packs);

    $query = new WP_Query(array(
                'post_type' => 'indpackage',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'orderby' => 'post_date',
                'order' => 'ASC'
                    )
    );

    $ind_form_box  = '<table class="form-table" border="1">';
    $ind_form_box  .= "<tr><th style=''>
                    <b>Select Package</b>
              </th>
              <td>
                    <b>Package Names</b>
              </td></tr>";

    while ($query->have_posts()) : $query->the_post();
        $checked_whichs = (in_array($query->post->ID, $ind_table_packs)) ? "checked" : "";
        $ind_form_box  .= "<tr><th style=''>
                    <input type='checkbox' name='pricing_ind_table_packages[]' $checked_whichs value='" . $query->post->ID . "' />
              </th><td>" . $query->post->post_title . " </td></tr>";

    endwhile;

    $ind_form_box  .= '</table>';

    echo $ind_form_box ;
}

What causing the error?

Save metabox function

function ind_save_pricing_boxtables($post_id) {

    if (!wp_verify_nonce($_POST['ind_pricing_table_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    if ('indtable' == $_POST['post_type'] && current_user_can('edit_post', $post_id)) {

        $pricing_ind_table_packages = (isset($_POST['pricing_ind_table_packages']) ? $_POST['pricing_ind_table_packages'] : array());
        $pricing_ind_table_packages = json_encode($pricing_ind_table_packages);

        update_post_meta($post_id, "_ind_meta_table_keypacks", $pricing_ind_table_packages);
    } else {
        return $post_id;
    }
}
2
  • 1
    i think the problem is in your nonce, can you post save metabox function code here to investigate the problem.
    – Anjum
    Aug 25, 2013 at 6:47
  • Yes! I also noticed about it after I researched the problem. And it seem the save metabox function won't be nonced. Updated the question with the save function.
    – Ari
    Aug 25, 2013 at 7:40

1 Answer 1

2

so you need to correct your nonce field add a second pram nonce name, more information on WordPress codex here

// wp nonce field
wp_nonce_field( $action, $name, $referer, $echo );

// replace yours with below
wp_nonce_field( 'ind_pricing_table_box_nonce', 'ind_pricing_nonce' );

now verify your nonce, more info here

wp_verify_nonce( $nonce, $action );

// replace yours with below
if (!wp_verify_nonce( $_POST['ind_pricing_nonce'], 'ind_pricing_table_box_nonce' )) {
    return $post_id;
}
5
  • Thank you! But as I said before, it seem the meta box can't be nonced. I received this message Notice: Undefined index: ind_pricing_nonce in /home4/myusername/public_html/mysite.com/wp-content/plugins/ind_css3_pricing_table/ind_css3_pricing_table_grid.php on line 445
    – Ari
    Aug 25, 2013 at 12:43
  • echo your nonce field echo wp_nonce_field( 'ind_pricing_table_box_nonce', 'ind_pricing_nonce' ); or wp_nonce_field( 'ind_pricing_table_box_nonce', 'ind_pricing_nonce', '', true );
    – Anjum
    Aug 25, 2013 at 13:02
  • Thank you for your effort, but it is still showing the notice above.
    – Ari
    Aug 25, 2013 at 13:33
  • can you show me code on line 445
    – Anjum
    Aug 25, 2013 at 14:23
  • It is the first line of this wp verify nonce: if (!wp_verify_nonce( $_POST['ind_pricing_nonce'], 'ind_pricing_table_box_nonce' )) { return $post_id;' }
    – Ari
    Aug 25, 2013 at 17:28

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.