Skip to content

Instantly share code, notes, and snippets.

@j26design
Forked from neilgee/a-cpt.php
Created March 24, 2016 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j26design/3afe7f795ba2ff74d5a3 to your computer and use it in GitHub Desktop.
Save j26design/3afe7f795ba2ff74d5a3 to your computer and use it in GitHub Desktop.
CPT (Custom Post Type) Genesis Theme - Plugin - WordPress Example
<?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
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
*/
add_action( 'init', 'create_custom_post_type' );
function 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 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.
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__, 'my_rewrite_flush' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment