我已经为post添加了几个元框,以及保存这些框的函数。
保存菜单时(外观->菜单)。它会为每个添加的菜单或子菜单抛出警告Notice: Undefined index: image_price_variation in /home1/xxx/public_html/xx/wp-content/plugins/xxx-shopping-cart/xxx_metabox_functions.php on line 89
虽然我正在保存菜单而不是帖子。
以下是代码的简短版本:
function add_custom_meta_boxes() {
/* Adding price meta box to post */
add_meta_box(
\'oap_price_box\',
\'Product Price\',
\'oap_price_mockup\',
\'post\',
\'side\',
\'high\'
);
// ...
}
add_action(\'add_meta_boxes\', \'add_custom_meta_boxes\');
/* Prints the price html mockup */
function oap_price_mockup( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), \'image_price_variation\' );
$productPrice = (double)get_post_meta( $post->ID, \'productPrice\', true );
$html = \'$ <input type="text" size="7" name="productPrice" placeholder="price" value="\'.(($productPrice != \'\') ? $productPrice : \'\').\'" />\';
echo $html;
}
function save_meta_boxes_post_data( $post ) {
// verification
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( !wp_verify_nonce( $_POST[\'image_price_variation\'], plugin_basename( __FILE__ ) ) ) // - This is line 89 ..
return;
if ( \'post\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_post\', $post ) )
return;
}
$post_ID = $_POST[\'post_ID\'];
/* Saveing Price*/
if(isset($_POST[\'productPrice\']) && $_POST[\'productPrice\'] != \'\'){
$price = (double)sanitize_text_field( $_POST[\'productPrice\'] );
update_post_meta($post_ID, \'productPrice\', $price);
}
}
add_action( \'save_post\', \'save_meta_boxes_post_data\' );
错误来自未显示的nonce字段,但是WP是否在保存后到菜单保存之间进行了分隔?如果是这样的话,我能做些什么来改变我的想法吗?