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.