admin管理员组文章数量:1327697
I've created a meta box. The code is:
/**
* Add meta box
*
* @param post $post The post object
* @link
*/
function portfolio_add_meta_boxes( $post ){
add_meta_box( 'portfolio_meta_box', __( 'Company URLs', 'portfolio' ), 'portfolio_build_meta_box', 'portfolio', 'side', 'low' );
}
add_action( 'add_meta_boxes_portfolio', 'portfolio_add_meta_boxes' );
/**
* Build custom field meta box
*
* @param post $post The post object
*/
function portfolio_build_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'portfolio_meta_box_nonce' );
// retrieve the current value
$fbportfolio = get_post_meta( $post->ID, 'fbportfolio', true );
$twportfolio = get_post_meta( $post->ID, 'twportfolio', true );
$instportfolio = get_post_meta( $post->ID, 'instaportfolio', true );
$linkinportfolio = get_post_meta( $post->ID, 'linkinportfolio', true );
?>
<div class='inside'>
<h3>Facebook</h3>
<p>
<input type="url" name="fbportfolio" value="<?php echo $fbportfolio; ?>">
</p>
</div>
<?php
}
/**
* Store custom field meta box data
*
* @param int $post_id The post ID.
* @link
*/
function portfolio_save_meta_box_data( $post_id ){
// store custom fields values
update_post_meta( get_the_ID(), 'fbportfolio', $_POST['fbportfolio'] );
}
add_action( 'save_post_food', 'portfolio_save_meta_box_data' );
I've created a meta box. The code is:
/**
* Add meta box
*
* @param post $post The post object
* @link https://codex.wordpress/Plugin_API/Action_Reference/add_meta_boxes
*/
function portfolio_add_meta_boxes( $post ){
add_meta_box( 'portfolio_meta_box', __( 'Company URLs', 'portfolio' ), 'portfolio_build_meta_box', 'portfolio', 'side', 'low' );
}
add_action( 'add_meta_boxes_portfolio', 'portfolio_add_meta_boxes' );
/**
* Build custom field meta box
*
* @param post $post The post object
*/
function portfolio_build_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'portfolio_meta_box_nonce' );
// retrieve the current value
$fbportfolio = get_post_meta( $post->ID, 'fbportfolio', true );
$twportfolio = get_post_meta( $post->ID, 'twportfolio', true );
$instportfolio = get_post_meta( $post->ID, 'instaportfolio', true );
$linkinportfolio = get_post_meta( $post->ID, 'linkinportfolio', true );
?>
<div class='inside'>
<h3>Facebook</h3>
<p>
<input type="url" name="fbportfolio" value="<?php echo $fbportfolio; ?>">
</p>
</div>
<?php
}
/**
* Store custom field meta box data
*
* @param int $post_id The post ID.
* @link https://codex.wordpress/Plugin_API/Action_Reference/save_post
*/
function portfolio_save_meta_box_data( $post_id ){
// store custom fields values
update_post_meta( get_the_ID(), 'fbportfolio', $_POST['fbportfolio'] );
}
add_action( 'save_post_food', 'portfolio_save_meta_box_data' );
Share
Improve this question
asked Jul 27, 2020 at 8:27
Khalid AlmallahiKhalid Almallahi
31 bronze badge
2 Answers
Reset to default 0add_action( 'save_post', ...
and test if your post type is tour custom post type.
You can avoid needing to test for the post type in the save_post
action by using save_post_{post_type}
E.g.
add_action( 'save_post_portfolio', .....
Where portfolio
is the post type being saved. Using save_post
will run your code for all posts types unless explicitly checked.
本文标签: Can39t save meta box data in Wordpress using custom post types
版权声明:本文标题:Can't save meta box data in Wordpress using custom post types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742230201a2437080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论