function register_custom_post_type() {
$args = array(
'label' => 'Post Type Name',
'description' => 'Description of Post Type',
'labels' => array(
'name' => 'Post Types',
'singular_name' => 'Post Type',
'menu_name' => 'Post Types',
'name_admin_bar' => 'Post Type',
'archives' => 'Item Archives',
'attributes' => 'Item Attributes',
'parent_item_colon' => 'Parent Item:',
'all_items' => 'All Items',
'add_new_item' => 'Add New Item',
'add_new' => 'Add New',
'new_item' => 'New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'view_item' => 'View Item',
'view_items' => 'View Items',
'search_items' => 'Search Item',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into item',
'uploaded_to_this_item' => 'Uploaded to this item',
'items_list' => 'Items list',
'items_list_navigation' => 'Items list navigation',
'filter_items_list' => 'Filter items list',
),
'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'custom-fields'),
'taxonomies' => array('category', 'post_tag'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search'=> false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rewrite' => array('slug' => 'post-type-slug'),
);
register_post_type('post_type_key', $args);
}
add_action('init', 'register_custom_post_type', 0);
Register Metabox
/**
* Register meta box(es) for our custom post type
*/
function add_custom_meta_boxes() {
add_meta_box(
'custom_meta_box', // Unique ID
'Additional Information', // Box title
'render_custom_meta_box', // Callback function
'your_post_type', // Post type
'normal', // Context (normal, side, advanced)
'high' // Priority (high, core, default, low)
);
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');
/**
* Render the meta box content
*/
function render_custom_meta_box($post) {
// Add nonce field for security
wp_nonce_field('custom_meta_box_nonce', 'meta_box_nonce');
// Get existing value from database
$value = get_post_meta($post->ID, '_custom_meta_key', true);
// Display the form field
echo '<label for="custom_field">';
echo 'Field Label:';
echo '</label> ';
echo '<input type="text" id="custom_field" name="custom_field" value="' . esc_attr($value) . '" size="25" />';
}
/**
* Save meta box content
*/
function save_custom_meta_box($post_id) {
// Check nonce
if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'custom_meta_box_nonce')) {
return;
}
// Check user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Save/update the field value
if (isset($_POST['custom_field'])) {
update_post_meta(
$post_id,
'_custom_meta_key',
sanitize_text_field($_POST['custom_field'])
);
}
}
add_action('save_post', 'save_custom_meta_box');
Advanced Fields
Dropdown
function render_select_meta_box($post) {
$selected = get_post_meta($post->ID, '_select_field', true);
?>
<label for="select_field">Choose option:</label>
<select name="select_field" id="select_field">
<option value="">Select...</option>
<option value="option1" <?php selected($selected, 'option1'); ?>>Option 1</option>
<option value="option2" <?php selected($selected, 'option2'); ?>>Option 2</option>
</select>
<?php
}
function save_select_meta_box($post_id) {
if (isset($_POST['select_field'])) {
update_post_meta($post_id, '_select_field', sanitize_text_field($_POST['select_field']));
}
}
Checkbox
function render_checkbox_meta_box($post) {
$checked = get_post_meta($post->ID, '_checkbox_field', true);
?>
<label for="checkbox_field">
<input type="checkbox" name="checkbox_field" id="checkbox_field" value="1" <?php checked($checked, '1'); ?> />
Enable this feature
</label>
<?php
}
function save_checkbox_meta_box($post_id) {
update_post_meta(
$post_id,
'_checkbox_field',
isset($_POST['checkbox_field']) ? '1' : '0'
);
}
WP Editor
function render_wysiwyg_meta_box($post) {
$content = get_post_meta($post->ID, '_wysiwyg_content', true);
wp_editor(
$content,
'wysiwyg_content',
array(
'textarea_name' => 'wysiwyg_content',
'media_buttons' => false,
'teeny' => true
)
);
}
function save_wysiwyg_meta_box($post_id) {
if (isset($_POST['wysiwyg_content'])) {
update_post_meta(
$post_id,
'_wysiwyg_content',
wp_kses_post($_POST['wysiwyg_content'])
);
}
}
Action Hooks
add_action('add_meta_boxes', 'add_custom_meta_boxes');
add_action('save_post', 'save_custom_meta_box');