我正在尝试创建一个自定义元框“设置你的情绪”,它将显示用户的情绪,但不幸的是,元框没有保存值。请帮忙
这是我的密码
<?php
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function nss_mood_add_meta_box() {
//$id, $title, $callback, $post_type, $context,$priority, $callback_args
add_meta_box(\'nss_mood_id\',\'Set your mood\',\'nss_mood_cb\',\'post\');
}
add_action(\'add_meta_boxes\',\'nss_mood_add_meta_box\');
// Dispalying form and taking input
function nss_mood_cb() {
wp_nonce_field(\'mood_meta_box\',\'mood_meta_box_nonce\');
$mood_value = get_post_meta( $post->ID, \'mood_value_key\', true );
// Creating our form
echo \'<p>\';
echo \'<label for="mood_value_key">What is your mood today ? </label></br></br>\';
echo \'<input type="text" class="widefat" name="mood_value_key" id="mood_value_key" value="\' . esc_attr( $mood_value ) . \'" />\';
echo \'</p>\';
}
// Checking value of the form and updating
function save_nss_mood_data($post_id) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST[\'mood_meta_box_nonce\'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[\'mood_meta_box_nonce\'], \'mood_meta_box\' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user\'s permissions.
if ( isset( $_POST[\'post_type\'] ) ) {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
/* OK, it\'s safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST[\'mood_field\'] ) ) {
return;
}
// Sanitize user input.
$mood_data = sanitize_text_field( $_POST[\'mood_value_key\'] );
// Update the meta field in the database.
update_post_meta( $post_id, \'mood_value_key\', $mood_data );
}
add_action(\'save_post\',\'save_nss_mood_data\');
?>