我正在尝试将一些元框和自定义帖子类型移动到插件文件中(以便在将来的主题更新中幸存下来)。一个问题是,现在我所有的wp\\u nonce值都返回了一个“未定义索引”通知(我意识到这不是一个大问题,但我希望我的代码以尽可能干净的方式工作)。然而,主要的问题是,save函数中的$\\u POST会导致“headers ready sent”的警告错误,与pluggables冲突。php文件(第876行)。下面是我尝试使用的代码:
// Add and Save Meta Boxes for Congregation Post Type
add_action( \'add_meta_boxes\', \'add_congregation_metaboxes\', 1 );
add_action(\'save_post\', \'save_address_meta\', 1, 2); // save the Address field
function add_congregation_metaboxes() {
add_meta_box(\'congregation_address\', \'Congregation Address\', \'congregation_address\', \'congregation\', \'side\', \'default\');
}
// The Congregation Address Metabox
function congregation_address() {
global $post;
// Noncename field
wp_nonce_field( plugin_basename( __FILE__ ), \'address_noncename\' );
// Get the first line of the address
$address_line1 = get_post_meta($post->ID, \'_address_line1\', true);
// Get second line of the address
$address_line2 = get_post_meta($post->ID, \'_address_line2\', true);
echo \'<p>Address</p>\';
echo \'<input type="text" name="_address_line1" value="\' . $address_line1 . \'" class="widefat" />\';
echo \'<input type="text" name="_address_line2" value="\' . $address_line2 . \'" class="widefat" />\';
}
// Save the Metabox Data !! The error is in the wp_verify_nonce
function save_address_meta($post_id, $post) {
// Verify Nonce
if ( !wp_verify_nonce( $_POST[\'address_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Check permissions
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
// Cycle through array
$address_meta[\'_address_line1\'] = $_POST[\'_address_line1\']; //This is the line that explodes
$address_meta[\'_address_line2\'] = $_POST[\'_address_line2\'];
// Add values of custom fields
foreach ($address_meta as $key => $value) { // Cycle array!
if( $post->post_type == \'revision\' ) return;
$value = implode(\',\', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
感谢您的帮助!