我正试图通过以下教程向我的主题添加自定义元框:https://www.sitepoint.com/adding-meta-boxes-post-types-wordpress/.
我可以看到meta框,它从数据库中获取值(通过手动更新post meta进行尝试)。然而,它从未得到更新。我尝试通过var\\u dump(ing)调试$_POST
数组,它显示变量值为空,但该值存在于$_REQUEST
. 有人知道原因吗?
调用缺少的变量post-excerpt
.这是我的密码。
function buziness_add_custom_box() {
// Adding layout meta box for page
add_meta_box(
\'offer-post-excerpt\',
esc_html__( \'Excerpt\', \'buziness\'),
\'buziness_post_excerpt\',
\'post\',
\'normal\',
\'default\'
);
}
接下来是回调函数:
function buziness_post_excerpt($post){
// Add a nonce field so we can check for it later
// Use nonce for verification
wp_nonce_field( basename( __FILE__ ) , \'custom_excerpt_nonce\' );
$value = get_post_meta($post->ID, \'_excerpt\', true );
echo \'<textarea style="width:100%" id="post_excerpt" name="post_excerpt">\' . esc_attr( $value ) . \'</textarea>\';
echo "<p>Excerpts are optional hand-crafted summaries of your content that can be used in your theme</p>";
}
这是连接到的函数save_post
钩
add_action(\'save_post\', \'buziness_save_post_excerpt\');
/**
* save the custom excerpt metabox data
* @hooked to save_post hook
*/
function buziness_save_post_excerpt($post_id) {
// Checks save status
var_dump($_POST);
var_dump($_REQUEST);
wp_die($_REQUEST);
$is_autosave = wp_is_post_autosave( $post_id );
$is_valid_nonce = ( isset( $_POST[ \'custom_excerpt_nonce\' ] ) && wp_verify_nonce( $_POST[ \'custom_excerpt_nonce\' ], basename( __FILE__ ) ) ) ? \'true\' : \'false\';
// Exits script depending on save status
if ( $is_autosave || !$is_valid_nonce ) {
return;
}
// Check the user\'s permissions.
if ( isset( $_POST[\'post_type\'] ) && \'post\' == $_POST[\'post_type\'] ) {
if ( ! current_user_can( \'edit_page\', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ \'post_excerpt\' ] ) ) {
update_post_meta( $post_id, \'_excerpt\', sanitize_text_field( $_POST[ \'post_excerpt\' ] ) );
}
}
以下是
var_dump($_POST)
\'ping_status\' => string \'open\' (length=4)
\'add_comment_nonce\' => string \'c641d598b3\' (length=10)
\'_ajax_fetch_list_nonce\' => string \'1e9f1f3f06\' (length=10)
\'post_name\' => string \'online-training\' (length=15)
\'post_author_override\' => string \'1\' (length=1)
\'custom_excerpt_nonce\' => string \'0cd0a833c4\' (length=10)
\'post_excerpt\' => string \'\' (length=0)
\'post_mime_type\' => string \'\' (length=0)
\'ID\' => int 96
以及
var_dump($_REQUEST)
\'ping_status\' => string \'open\' (length=4)
\'add_comment_nonce\' => string \'c641d598b3\' (length=10)
\'_ajax_fetch_list_nonce\' => string \'1e9f1f3f06\' (length=10)
\'post_name\' => string \'online-training\' (length=15)
\'post_author_override\' => string \'1\' (length=1)
\'custom_excerpt_nonce\' => string \'0cd0a833c4\' (length=10)
\'post_excerpt\' => string \'TES t \' (length=6)
正如您所看到的,\'
post_excerpt
\' 的输出中为空
$_POST
.