Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Custom Post Type
<?php
/**
* Plugin Name: Event Post Types
* Plugin URI: https://blog.dimasc.io/wordpress/creating-custom-post-types
* Description: A tutorial on creating custom post types wit WordPress.
* Version: 1.0
* Author: Ryan DiMascio
* Author URI: https://ryandimascio.com
* License: GPL2
*/
// Create Event Post Type
function create_event_post_type() {
$labels = array(
'name' => 'Events',
'singular_name' => 'Event',
'add_new' => 'Add New Event',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'new_item' => 'New Event',
'all_items' => 'All Events',
'view_item' => 'View Event',
'search_items' => 'Search Events',
'not_found' => 'No Events Found',
'not_found_in_trash' => 'No Events found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Events',
);
// Register Event Post Type
register_post_type( 'event', array(
'labels' => $labels,
'has_archive' => true,
'has_rest' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'publicly_queryable' => true,
'menu_icon' => 'dashicons-tickets',
'menu_position' => 5,
'description' => 'Events custom post type example for GitHub',
'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
'taxonomies' => array( 'event_tag', 'event_category' ),
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'events' )
)
);
}
add_action( 'init', 'create_event_post_type' );
// Edit Event Post Type Title Text
function event_title_text( $title ) {
$screen = get_current_screen();
if ( 'event' == $screen->post_type ) {
$title = 'Enter name of the event here';
}
return $title;
}
add_filter( 'enter_title_here', 'event_title_text' );
// Create Event Post Type Categories
function create_event_categories() {
$labels = array(
'name' => _x( 'Event Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Event Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Event Categories' ),
'all_items' => __( 'All Event Categories' ),
'parent_item' => __( 'Parent Event Category' ),
'parent_item_colon' => __( 'Parent Event Category:' ),
'edit_item' => __( 'Edit Event Category' ),
'update_item' => __( 'Update Event Category' ),
'add_new_item' => __( 'Add New Event Category' ),
'new_item_name' => __( 'New Event Category' ),
'menu_name' => __( 'Event Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'event_category', 'event', $args );
}
add_action( 'init', 'create_event_categories', 0 );
// Create Event Post Type Tags
function create_event_tags() {
$labels = array(
'name' => _x( 'Event Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Event Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Event Tags' ),
'all_items' => __( 'All Event Tags' ),
'parent_item' => __( 'Parent Event Tag' ),
'parent_item_colon' => __( 'Parent Event Tag:' ),
'edit_item' => __( 'Edit Event Tag' ),
'update_item' => __( 'Update Event Tag' ),
'add_new_item' => __( 'Add New Event Tag' ),
'new_item_name' => __( 'New Event Tag' ),
'menu_name' => __( 'Event Tags' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'event_tag', 'event', $args );
}
add_action( 'init', 'create_event_tags', 0 );
// Edit Event Post Type Messages
function create_event_messages( $messages ) {
global $post, $post_ID;
$link = esc_url( get_permalink($post_ID) );
$messages['event'] = array(
0 => '',
1 => sprintf( __('Event updated. <a href="%s">View event</a>'), $link ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Event updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Event restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Event published. <a href="%s">View event</a>'), $link ),
7 => __('Event saved.'),
8 => sprintf( __('Event submitted. <a target="_blank" href="%s">Preview event</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Event scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview event</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), $link ),
10 => sprintf( __('Event draft updated. <a target="_blank" href="%s">Preview event</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'create_event_messages' );
// Edit Event Post Type Bulk Messages
function event_bulk_messages( $bulk_messages, $bulk_counts ) {
$bulk_messages['event'] = array(
'updated' => _n( "%s event updated.", "%s events updated.", $bulk_counts["updated"] ),
'locked' => _n( "%s event not updated, somebody is editing it.", "%s events not updated, somebody is editing them.", $bulk_counts["locked"] ),
'deleted' => _n( "%s event permanently deleted.", "%s events permanently deleted.", $bulk_counts["deleted"] ),
'trashed' => _n( "%s event moved to the Trash.", "%s events moved to the Trash.", $bulk_counts["trashed"] ),
'untrashed' => _n( "%s event restored from the Trash.", "%s events restored from the Trash.", $bulk_counts["untrashed"] ),
);
return $bulk_messages;
}
add_filter( 'bulk_post_updated_messages', 'event_bulk_messages', 10, 2 );
// Edit Event Post Type Help Text
function event_help() {
$screen = get_current_screen();
if ( 'event' != $screen->post_type )
return;
$args = [
'id' => 'event_help',
'title' => 'Event Help',
'content' => '<h3>Events</h3><p>Create, edit, and publish events.</p>',
];
$screen->add_help_tab( $args );
}
add_action('admin_head', 'plugin_help' );
<?php
/**
* Plugin Name: Event Post Types
* Plugin URI: https://blog.dimasc.io/wordpress/creating-custom-post-types
* Description: A tutorial on creating custom post types wit WordPress.
* Version: 1.0
* Author: Ryan DiMascio
* Author URI: https://ryandimascio.com
* License: GPL2
*/
// Create Event Post Type
function create_event_post_type() {
$labels = array(
'name' => 'Events',
'singular_name' => 'Event',
'add_new' => 'Add New Event',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'new_item' => 'New Event',
'all_items' => 'All Events',
'view_item' => 'View Event',
'search_items' => 'Search Events',
'not_found' => 'No Events Found',
'not_found_in_trash' => 'No Events found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Events',
);
// Register Event Post Type
register_post_type( 'event', array(
'labels' => $labels,
'has_archive' => true,
'has_rest' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'publicly_queryable' => true,
'menu_icon' => 'dashicons-tickets',
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
'taxonomies' => array( 'event_tag', 'event_category' ),
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'events' )
)
);
}
add_action( 'init', 'create_event_post_type' );
// Edit Event Post Type Title Text
function event_title_text( $title ) {
$screen = get_current_screen();
if ( 'event' == $screen->post_type ) {
$title = 'Enter name of the event here';
}
return $title;
}
add_filter( 'enter_title_here', 'event_title_text' );
// Create Event Post Type Categories
function create_event_categories() {
$labels = array(
'name' => _x( 'Event Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Event Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Event Categories' ),
'all_items' => __( 'All Event Categories' ),
'parent_item' => __( 'Parent Event Category' ),
'parent_item_colon' => __( 'Parent Event Category:' ),
'edit_item' => __( 'Edit Event Category' ),
'update_item' => __( 'Update Event Category' ),
'add_new_item' => __( 'Add New Event Category' ),
'new_item_name' => __( 'New Event Category' ),
'menu_name' => __( 'Event Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'event_category', 'event', $args );
}
add_action( 'init', 'create_event_categories', 0 );
// Edit Plugin Post Type Messages
function event_messages( $messages ) {
global $post, $post_ID;
$link = esc_url( get_permalink($post_ID) );
$messages['plugin'] = array(
0 => '',
1 => sprintf( __('Plugin updated. <a href="%s">View plugin</a>'), $link ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Plugin updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Plugin restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Plugin published. <a href="%s">View plugin</a>'), $link ),
7 => __('Plugin saved.'),
8 => sprintf( __('Plugin submitted. <a target="_blank" href="%s">Preview plugin</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Plugin scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview plugin</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), $link ),
10 => sprintf( __('Plugin draft updated. <a target="_blank" href="%s">Preview plugin</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'event_messages' );
// Edit Event Post Type Bulk Messages
function event_bulk_messages( $bulk_messages, $bulk_counts ) {
$bulk_messages['event'] = array(
'updated' => _n( "%s event updated.", "%s events updated.", $bulk_counts["updated"] ),
'locked' => _n( "%s event not updated, somebody is editing it.", "%s events not updated, somebody is editing them.", $bulk_counts["locked"] ),
'deleted' => _n( "%s event permanently deleted.", "%s events permanently deleted.", $bulk_counts["deleted"] ),
'trashed' => _n( "%s event moved to the Trash.", "%s events moved to the Trash.", $bulk_counts["trashed"] ),
'untrashed' => _n( "%s event restored from the Trash.", "%s events restored from the Trash.", $bulk_counts["untrashed"] ),
);
return $bulk_messages;
}
add_filter( 'bulk_post_updated_messages', 'event_bulk_messages', 10, 2 );
// Edit Event Post Type Help Text
function event_help() {
$screen = get_current_screen();
if ( 'event' != $screen->post_type )
return;
$args = [
'id' => 'event_help',
'title' => 'Event Help',
'content' => '<h3>Events</h3><p>Create, edit, and publish events.</p>',
];
$screen->add_help_tab( $args );
}
add_action('admin_head', 'plugin_help' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment