无法在Wordpress中自动保存自定义字段
问题描述:
我一步一步地使用Smashing Magazine教程。但是在自定义字段中不存在自动保存。点击发布并更新按钮后,这些字段为空。如何解决问题。无法在Wordpress中自动保存自定义字段
function my_custom_post_movie() {
$labels = array(
'name' => _x('movies', 'post type general name'),
'singular_name' => _x('movie', 'post type singular name'),
'add_new' => _x('Add New', 'book'),
'add_new_item' => __('Add New movie'),
'edit_item' => __('Edit movie'),
'new_item' => __('New movie'),
'all_items' => __('All movies'),
'view_item' => __('View movie'),
'search_items' => __('Search movies'),
'not_found' => __('No movies found'),
'not_found_in_trash' => __('No movies found in the Trash'),
'parent_item_colon' => '',
'menu_name' => 'movies'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our movies and movie specific data',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'has_archive' => true,
);
register_post_type('movie', $args);
}
add_action('init', 'my_custom_post_movie');
// Register Custom Taxonomies
function my_taxonomies_movie() {
$labels = array(
'name' => _x('movie Categories', 'taxonomy general name'),
'singular_name' => _x('movie Category', 'taxonomy singular name'),
'search_items' => __('Search movie Categories'),
'all_items' => __('All movie Categories'),
'parent_item' => __('Parent movie Category'),
'parent_item_colon' => __('Parent movie Category:'),
'edit_item' => __('Edit movie Category'),
'update_item' => __('Update movie Category'),
'add_new_item' => __('Add New movie Category'),
'new_item_name' => __('New movie Category'),
'menu_name' => __('movie Categories'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy('movie_category', 'movie', $args);
}
add_action('init', 'my_taxonomies_movie', 0);
// Meta Box
add_action('add_meta_boxes', 'movie_date_box');
function movie_date_box() {
add_meta_box(
'movie_date_box',
__('movie date', 'myplugin_textdomain'),
'movie_date_box_content',
'movie',
'side',
'high'
);
}
function movie_date_box_content($post) {
wp_nonce_field(plugin_basename(__FILE__), 'movie_date_box_content_nonce');
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
add_action('save_post', 'movie_date_box_save');
function movie_date_box_save($post_id) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!wp_verify_nonce($_POST['movie_date_box_content_nonce'], plugin_basename(__FILE__)))
return;
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return;
} else {
if (!current_user_can('edit_post', $post_id))
return;
}
$movie_date = $_POST['movie_date'];
update_post_meta($post_id, 'movie_date', $movie_date);
}
感谢您的帮助。
答
您的信息实际上被保存到数据库中,但是一旦帖子被提交,它就不会被调用。
变化:
function movie_date_box_content($post) {
wp_nonce_field(plugin_basename(__FILE__), 'movie_date_box_content_nonce');
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
以下几点:
function movie_date_box_content($post) {
wp_nonce_field(plugin_basename(__FILE__), 'movie_date_box_content_nonce');
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" value="' . get_post_meta($post->ID, 'movie_date', true) . '" />';
}
你失踪value="' . get_post_meta($post->ID, 'movie_date', true) . '"
get_post_meta()
调用从数据库中的元关键movie_date
并将其显示,如果有保存什么在那个关键。
而且改变:
if (!wp_verify_nonce($_POST['movie_date_box_content_nonce'], plugin_basename(__FILE__)))
return;
以下几点:
if (!isset($_POST['movie_date_box_content_nonce']) || !wp_verify_nonce($_POST['movie_date_box_content_nonce'], plugin_basename(__FILE__)))
return;
它检查是否现时字段设置,而不是NULL。它会照顾未声明索引的通知,该通知显示在管理员中。