Create a gist now

Instantly share code, notes, and snippets.

CPT (Custom Post Type) Genesis Theme - Plugin - WordPress Example - there are 2 snippets here - the one name - cpt-genesis-hide.php hides the single and archive views, the other one is normal
<?php
/*
Plugin Name: Event Custom Post Type
Plugin URI: http://wpbeaches.com/create-custom-post-types-in-genesis-child-theme-in-wordpress/
Description: Custom Post Types for Event
Author: Neil Gee
Version:1.0.0
Author URI:http://wpbeaches.com
*/
/*
This code is a plugin to create a Custom Post Type in WordPress, it can be used with any WordPress theme.
The action initialises the function below it.
This example uses the term 'Events' as its name, a search and replace will allow any name to be used, making sure plural and singular versions of the name are replaced.
Also replace the name in 'rewrite' and in the 'register_post_type' function.
For non-Genesis themes the 'genesis-cpt-archives-settings' can be removed from the supports array.
To activate this as a plugin just add to wp-contents/plugins and activate in Dashboard
This doesn't use all the labels and arguments possible but includes the main ones, you can see more here - https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'themeprefix_create_custom_post_type' );
function themeprefix_create_custom_post_type() {
$labels = array(
'name' => __( 'Event' ),
'singular_name' => __( 'Event' ),
'all_items' => __( 'All Events' ),
'add_new' => _x( 'Add new Event', 'Events' ),
'add_new_item' => __( 'Add new Event' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search in Events' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-admin-users', //pick one here ~> https://developer.wordpress.org/resource/dashicons/
'rewrite' => array( 'slug' => 'event' ),
'taxonomies' => array( 'category', 'post_tag' ),
'query_var' => true,
'menu_position' => 5,
'supports' => array( 'genesis-cpt-archives-settings', 'thumbnail' , 'custom-fields', 'excerpt', 'comments', 'title', 'editor')
);
register_post_type( 'event', $args );
}
// flush the permalinks - ref - https://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
function themeprefix_my_rewrite_flush() {
// First, we "add" the custom post type via the above written function.
// Note: "add" is written with quotes, as CPTs don't get added to the DB,
// They are only referenced in the post_type column with a post entry,
// when you add a post of this CPT.
themeprefix_create_custom_post_type();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'themeprefix_my_rewrite_flush' );
<?php
/*
Plugin Name: Event Custom Post Type
Plugin URI: http://wpbeaches.com/create-custom-post-types-in-genesis-child-theme-in-wordpress/
Description: Custom Post Types for Event
Author: Neil Gee
Version:1.0.0
Author URI:http://wpbeaches.com
*/
/*
This code is a plugin to create a Custom Post Type in WordPress, it can be used with any WordPress theme.
The action initialises the function below it.
This example uses the term 'Events' as its name, a search and replace will allow any name to be used, making sure plural and singular versions of the name are replaced.
Also replace the name in 'rewrite' and in the 'register_post_type' function.
For non-Genesis themes the 'genesis-cpt-archives-settings' can be removed from the supports array.
To activate this as a plugin just add to wp-contents/plugins and activate in Dashboard
This doesn't use all the labels and arguments possible but includes the main ones, you can see more here - https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'themeprefix_create_custom_post_type' );
function themeprefix_create_custom_post_type() {
$labels = array(
'name' => __( 'Event' ),
'singular_name' => __( 'Event' ),
'all_items' => __( 'All Events' ),
'add_new' => _x( 'Add new Event', 'Events' ),
'add_new_item' => __( 'Add new Event' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search in Events' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in trash' ),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false, // Set to false hides Archive Pages
'menu_icon' => 'dashicons-admin-users', //pick one here ~> https://developer.wordpress.org/resource/dashicons/
'rewrite' => array( 'slug' => 'event' ),
'taxonomies' => array( 'category', 'post_tag' ),
'query_var' => true,
'menu_position' => 5,
'publicly_queryable' => false, // Set to false hides Single Pages
'supports' => array( 'genesis-cpt-archives-settings', 'thumbnail' , 'custom-fields', 'excerpt', 'comments', 'title', 'editor')
);
register_post_type( 'event', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment