Create a custom post type and its metaboxes

In WordPress everything is a post_type and anything you would like to attach with this post_type is possible by metaboxes. Posts are stored in the wp_post table and its meta information is stored in wp_postmeta table.

This is a very basic structure which WordPress follows and every new collection is build around it.

posts and pages are the default post_types that come with the WP install.

Let we simplify this by an example.

Requirements: We need to build an collection of events and event will include a title, description, event_date, venue and an thumbnail Image.

By default we can call post-title,description,and thumbnail property of a post type.

Lets create a post type events.

// Registers the new post type and taxonomy

function wpt_event_posttype() {
register_post_type( ‘events’,
array(
‘labels’ => array(
‘name’ => __( ‘Events’ ),
‘singular_name’ => __( ‘Event’ ),
‘add_new’ => __( ‘Add New Event’ ),
‘add_new_item’ => __( ‘Add New Event’ ),
‘edit_item’ => __( ‘Edit Event’ ),
‘new_item’ => __( ‘Add New Event’ ),
‘view_item’ => __( ‘View Event’ ),
‘search_items’ => __( ‘Search Event’ ),
‘not_found’ => __( ‘No events found’ ),
‘not_found_in_trash’ => __( ‘No events found in trash’ )
),
‘public’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘comments’ ),
‘capability_type’ => ‘post’,
‘rewrite’ => array(“slug” => “events”), // Permalinks format
‘menu_position’ => 5,
‘register_meta_box_cb’ => ‘add_events_metaboxes’
)
);
}

add_action( ‘init’, ‘wpt_event_posttype’ );

Supports is used to call default options like title, editor,thumbnail and comments.

  • Rewrite will assign a slug for listing page for events and that can be called by a ‘archieve.php’ template.

  • If you want to use a separate template make a copy of archieve.php and rename it ‘events.php’ as it follows the priority {custom-post-type}.php

register_meta_box_cb will decide which function is to be call for meta boxes.

Lets set an action for events metaboxes

add_action( ‘add_meta_boxes’, ‘add_events_metaboxes’ );

Add meta box:

Now following function will add meta box.

// Add the Events Meta Boxes

function add_events_metaboxes() {
add_meta_box(‘wpt_events_date’, ‘Event Date’, ‘wpt_events_date’, ‘events’, ‘side’, ‘default’);
add_meta_box(‘wpt_events_location’, ‘Event Location’, ‘wpt_events_location’, ‘events’, ‘normal’, ‘high’);
}

Above code is adding two metaboxes while we are following one metabox that will be used to store two meta values

Generation HTML for Meta boxes:

// The Event Location Metabox

function wpt_events_location() {
global $post;

// Noncename needed to verify where the data originated
echo ‘<input type=”hidden” name=”eventmeta_noncename” id=”eventmeta_noncename” value=”‘ .
wp_create_nonce( plugin_basename(__FILE__) ) . ‘” />’;

// Get the location data if its already been entered
$location = get_post_meta($post->ID, ‘_location’, true);
$eventdate= get_post_meta($post->ID, ‘_eventdate’, true);

// Echo out the field
echo ‘<p>Enter the location:</p>’;
echo ‘<input type=”text” name=”_location” value=”‘ . $location . ‘” class=”widefat” />’;
echo ‘<p>How Should People Dress?</p>’;
echo ‘<input type=”text” name=”_eventdate” value=”‘ . $eventdate. ‘” class=”widefat” />’;

}

Saving meta data:

// Save the Metabox Data

function wpt_save_events_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[‘eventmeta_noncename’], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( ‘edit_post’, $post->ID ))
return $post->ID;

// OK, we’re authenticated: we need to find and save the data
// We’ll put it into an array to make it easier to loop though.

$events_meta[‘_location’] = $_POST[‘_location’];
$events_meta[‘_eventdate’] = $_POST[‘_eventdate’];

// Add values of $events_meta as custom fields

foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == ‘revision’ ) return; // Don’t store custom data twice
$value = implode(‘,’, (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn’t have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

}

add_action(‘save_post’, ‘wpt_save_events_meta’, 1, 2); // save the custom fields

Now you have successfully created a metabox for a custom post type which is storing two values ‘location’ and ‘eventdate’. Try by saving this inside of event custom post_type.

You can call a metabox information by post id:

echo $location = get_post_meta($post->ID, ‘_location’, true);

 

Leave a Comment