Wordpress 自定义 Post 类型元框未保存到数据库
Wordpress Custom Post Type Meta Box not saving to database
我创建了一个自定义 Post 类型 'media-page-items',我正在尝试向其中添加元框。当前元框正在显示但未保存到数据库。我已经尝试了几种不同的方法,其中 none 似乎可以保存到数据库中。
调试已打开,但目前我没有看到任何错误被抛出。
非常感谢任何帮助!
//add article link to media page item
add_action( 'admin_menu', 'gruman_article_link_create' );
add_action( 'save_post', 'gruman_article_link_save', 10, 2 );
function gruman_article_link_create() {
add_meta_box( 'gruman-article-link', 'Article Link', 'gruman_article_link', 'media-page-items', 'advanced', 'high' );
}
function gruman_article_link( $post ) {
// retrieve the _gruman_article_title current value
$current_article_link = get_post_meta( $post->ID, '_gruman_article_link', true );
?>
<p>
<label>Article Link</label>
<br />
<input name="gruman-article-link" id="article-link" style="width: 97%;"><?php $current_article_link; ?>/>
<input type="hidden" name="gruman_article_link_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
function gruman_article_link_save( $post_id ) {
// verify taxonomies meta box nonce
if ( !isset( $_POST['gruman_article_link_nonce'] ) || !wp_verify_nonce( $_POST['gruman_article_link_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return $post_id;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ){
return $post_id;
}
// store article title value
if ( isset( $_REQUEST['gruman-article-link'] ) ) {
update_post_meta( $post_id, '_gruman_article_link', sanitize_text_field( $_POST['gruman-article-link'] ) );
}
}
在 wp_create_nonce 中,您正在使用 plugin_basename( __FILE__ )
。
当您验证随机数时,您使用 basename( __FILE__ )
作为操作名称。
这些值是不一样的。第一个将 return 类似于 my-plugin/my-plugin.php
,第二个将是 my-plugin.php
这就是为什么我认为 wp_verify_nonce return 是假的,你的数据没有保存。
我创建了一个自定义 Post 类型 'media-page-items',我正在尝试向其中添加元框。当前元框正在显示但未保存到数据库。我已经尝试了几种不同的方法,其中 none 似乎可以保存到数据库中。
调试已打开,但目前我没有看到任何错误被抛出。
非常感谢任何帮助!
//add article link to media page item
add_action( 'admin_menu', 'gruman_article_link_create' );
add_action( 'save_post', 'gruman_article_link_save', 10, 2 );
function gruman_article_link_create() {
add_meta_box( 'gruman-article-link', 'Article Link', 'gruman_article_link', 'media-page-items', 'advanced', 'high' );
}
function gruman_article_link( $post ) {
// retrieve the _gruman_article_title current value
$current_article_link = get_post_meta( $post->ID, '_gruman_article_link', true );
?>
<p>
<label>Article Link</label>
<br />
<input name="gruman-article-link" id="article-link" style="width: 97%;"><?php $current_article_link; ?>/>
<input type="hidden" name="gruman_article_link_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
function gruman_article_link_save( $post_id ) {
// verify taxonomies meta box nonce
if ( !isset( $_POST['gruman_article_link_nonce'] ) || !wp_verify_nonce( $_POST['gruman_article_link_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return $post_id;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ){
return $post_id;
}
// store article title value
if ( isset( $_REQUEST['gruman-article-link'] ) ) {
update_post_meta( $post_id, '_gruman_article_link', sanitize_text_field( $_POST['gruman-article-link'] ) );
}
}
在 wp_create_nonce 中,您正在使用 plugin_basename( __FILE__ )
。
当您验证随机数时,您使用 basename( __FILE__ )
作为操作名称。
这些值是不一样的。第一个将 return 类似于 my-plugin/my-plugin.php
,第二个将是 my-plugin.php
这就是为什么我认为 wp_verify_nonce return 是假的,你的数据没有保存。