I’ve created a meta box. The code is:
/**
* Add meta box
*
* @param post $post The post object
* @link https://codex.wordpress.org/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.org/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' );
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
add_action( 'save_post', ...
and test if your post type is tour custom post type.
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0