我的自定义帖子类型有一个元框,它在元框中列出了另一个自定义帖子类型。每次我尝试发布具有该自定义帖子类型的帖子时,它都会导致错误。它总是出现在一个页面上,上面写着“你确定要这样做吗?请再试一次”。它在我的localhost中没有任何问题,但在我的web主机上运行时不起作用。
以下是代码:
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 ;
}
导致错误的原因是什么?
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;
}
}
SO网友:Anjum
因此,您需要更正nonce字段,添加第二个pram nonce名称,有关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\' );
现在验证您的nonce,了解更多信息
herewp_verify_nonce( $nonce, $action );
// replace yours with below
if (!wp_verify_nonce( $_POST[\'ind_pricing_nonce\'], \'ind_pricing_table_box_nonce\' )) {
return $post_id;
}