In previous situations I have been able to add a custom meta field to posts, the code was this:
function website_add_meta_box() {
add_meta_box(
'website-website',
__('Website', 'website'),
'website_html',
'post',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'website_add_meta_box');
function website_html($post) {
wp_nonce_field('_website_nonce', 'website_nonce'); ?>
<p>Information about users website.</p>
<p>
<label for="website_website_url"><?php _e('Website URL', 'website'); ?></label><br>
<input type="text" name="website_website_url" id="website_website_url" value="<?php echo website_get_meta('website_website_url'); ?>">
</p>
<p>
<label for="website_website_source_code"><?php _e('Website Source Code', 'website'); ?></label><br>
<textarea name="website_website_source_code" id="website_website_source_code" ><?php echo website_get_meta('website_website_source_code'); ?></textarea>
</p><?php
}
function website_save($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['website_nonce']) || !wp_verify_nonce($_POST['website_nonce'], '_website_nonce' )) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['website_website_url']))
update_post_meta($post_id, 'website_website_url', esc_attr($_POST['website_website_url']));
if (isset($_POST['website_website_source_code']))
update_post_meta($post_id, 'website_website_source_code', esc_attr($_POST['website_website_source_code']));
}
add_action('save_post', 'website_save');
My question is how can this code be adapted to apply only to the custom post type of ‘website’?
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
The fourth parameter is $screen, which in your case is set to post, so all you have to do is specify the website post type instead:
function website_add_meta_box() {
add_meta_box(
'website-website',
__('Website', 'website'),
'website_html',
'website',
'normal',
'default'
);
}
Documentation here: https://developer.wordpress.org/reference/functions/add_meta_box/
As an aside, your save_post hook applies to any post or custom post type being saved, but if website_website_url and website_website_source_code only apply to the website post type, you can add an additional check:
function website_save($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['website_nonce']) || !wp_verify_nonce($_POST['website_nonce'], '_website_nonce' )) return;
if (!current_user_can('edit_post', $post_id)) return;
if (get_post_type($post_id) !== 'website') return;
if (isset($_POST['website_website_url']))
update_post_meta($post_id, 'website_website_url', esc_attr($_POST['website_website_url']));
if (isset($_POST['website_website_source_code']))
update_post_meta($post_id, 'website_website_source_code', esc_attr($_POST['website_website_source_code']));
}
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