我创建了一个Posteta框,但它似乎没有将任何内容保存到DB。我正在检查wp\\U Posteta,没有任何与此元数据库相关的内容。
我认为这是由于die
显示错误。
问题:是什么导致我的metabox无法保存到数据库,因此不允许我输出结果。
这是我创建/保存元数据库的代码。
<?php
// Add the Meta Box
function ge_add_subtitle_meta_box() {
add_meta_box(
\'excerpt_meta_box\', // $id
\'Excerpt Meta Box\', // $title
\'ge_show_subtitle_meta_box\', // $callback
\'page\', // $page
\'normal\', // $context
\'high\'); // $priority
}
//hook the function to add meta boxes
add_action(\'add_meta_boxes\', \'ge_add_subtitle_meta_box\');
// Field Array
$prefix = \'getextra\';
$custom_meta_fields = array(
array(
\'label\' => \'Subtitle\',
\'desc\' => \'Insert a small description of the page underneath this title.\',
\'id\' => $prefix.\'subtitle\',
\'type\' => \'textarea\'
)
);
// The Callback
function ge_show_subtitle_meta_box() {
//make array globalscope and call $post to acces global array
global $custom_meta_fields, $post;
// Use nonce for verification
echo \'<input type="hidden" name="subtitle_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
// Begin the field table and loop
echo \'<table class="form-table">\';
foreach ($custom_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field[\'id\'], true);
// begin a table row with
echo \'<tr>
<th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
<td>\';
switch($field[\'type\']) {
// case items will go here
// text
case \'text\':
echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// textarea
case \'textarea\':
echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
<br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
// checkbox
case \'checkbox\':
echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
<label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
break;
// select
case \'select\':
echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
foreach ($field[\'options\'] as $option) {
echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
}
echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
break;
} //end switch
echo \'</td></tr>\';
} // end foreach
echo \'</table>\'; // end table
}
// Save the Data
function getextra_save_subtitle_meta($post_id) {
global $custom_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST[\'subtitle_meta_box_nonce\'], basename(__FILE__))) {
return $post_id; } else { die( \'failed to verify nonce\' ); }
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if (\'page\' == $_POST[\'post_type\']) {
if (!current_user_can(\'edit_page\', $post_id))
return $post_id;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_fields as $field) {
$old = get_post_meta($post_id, $field[\'id\'], true);
$new = $_POST[$field[\'id\']];
if ($new && $new != $old) {
update_post_meta($post_id, $field[\'id\'], $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, $field[\'id\'], $old);
}
} // end foreach
}
//hook into save the post
add_action(\'save_post\', \'ge_save_subtitle_meta\');
?>