Last active
September 15, 2024 06:51
-
-
Save neilgee/8926914 to your computer and use it in GitHub Desktop.
CPT (Custom Post Type) - WordPress Plugin - there are 2 snippets here - the one name - cpt-hide.php hides the single and archive views, the other one is normal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Testimonials Custom Post Type | |
Plugin URI: http://wpbeaches.com/create-custom-post-types-in-genesis-child-theme-in-wordpress/ | |
Description: Testimonials Custom Post Types | |
Author: Neil Gowran | |
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 'Testimonials' 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. | |
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' => __( 'Testimonials' ), | |
'singular_name' => __( 'Testimonial' ), | |
'all_items' => __( 'All Testimonials' ), | |
'add_new' => _x( 'Add new Testimonial', 'Testimonials' ), | |
'add_new_item' => __( 'Add new Testimonial' ), | |
'edit_item' => __( 'Edit Testimonial' ), | |
'new_item' => __( 'New Testimonial' ), | |
'view_item' => __( 'View Testimonial' ), | |
'search_items' => __( 'Search in Testimonials' ), | |
'not_found' => __( 'No Testimonials found' ), | |
'not_found_in_trash' => __( 'No Testimonials found in trash' ), | |
'parent_item_colon' => '' | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'has_archive' => true, | |
'menu_icon' => 'dashicons-format-quote', //pick one here ~> https://developer.wordpress.org/resource/dashicons/ | |
'rewrite' => array( 'slug' => 'testimonials' ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'query_var' => true, | |
'menu_position' => 5, | |
'capability_type' => 'page', | |
'supports' => array( 'thumbnail' , 'custom-fields', 'excerpt', 'title', 'editor') | |
); | |
register_post_type( 'testimonial', $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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 Gowran | |
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. | |
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, | |
'capability_type' => 'page', | |
'publicly_queryable' => false, // Set to false hides Single Pages | |
'supports' => array( '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