Skip to content

Instantly share code, notes, and snippets.

@jamesmrobinson
Last active March 16, 2017 10:09
Show Gist options
  • Save jamesmrobinson/316a29936e5e7d709665 to your computer and use it in GitHub Desktop.
Save jamesmrobinson/316a29936e5e7d709665 to your computer and use it in GitHub Desktop.
A custom post type plugin for WordPress. The plugin will add the custom post type Projects and these posts will also feature a metabox that allows you to toggle whether the post is featured.
<?php
/**
* Plugin Name: Custom Post Types
* Plugin URI: https://gist.github.com/nosquirrelbones/316a29936e5e7d709665
* Description: A simple plugin (template) to register custom post types
* Version: 1.1
* Author: James Robinson
* Author URI: http://jmr.codes/
* License: GPL2
*/
/*
Copyright 2017 James Robinson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !defined( 'ABSPATH' ) ) exit;
function jmr_custom_posts() {
// ************* CLIENTS *************************************** //
// ** THIS IS A HIDDEN POST TYPE FOR BACKEND USE. ************** //
$client_labels = array(
'name' => __( 'Client', 'post type general name', 'jmr-cpt' ),
'singular_name' => __( 'Client', 'post type singular name', 'jmr-cpt' ),
'menu_name' => __( 'Clients', 'admin menu', 'jmr-cpt' ),
'name_admin_bar' => __( 'Client', 'add new on admin bar', 'jmr-cpt' ),
'add_new' => __( 'Add New', 'client', 'jmr-cpt' ),
'add_new_item' => __( 'Add New Client', 'jmr-cpt' ),
'edit_item' => __( 'Edit Client', 'jmr-cpt' ),
'new_item' => __( 'New Client', 'jmr-cpt' ),
'all_items' => __( 'All Clients', 'jmr-cpt' ),
'view_item' => __( 'View Client', 'jmr-cpt' ),
'search_items' => __( 'Search Client', 'jmr-cpt' ),
'not_found' => __( 'No clients found', 'jmr-cpt' ),
'not_found_in_trash' => __( 'No clients found in the Trash', 'jmr-cpt' ),
'parent_item_colon' => '',
'menu_name' => 'Clients'
);
$client_args = array(
'labels' => $client_labels,
'description' => 'Contains client logos and links',
'public' => false,
'show_ui' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-businessman',
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'rewrite' => false,
'has_archive' => false,
'hierarchical' => false
);
register_post_type( 'client', $client_args );
// ************* TESTIMONIAL *************************************** //
// ** THIS IS A PUBLICLY VISIBLE POST TYPE WITH REWRITES, ETC. ***** //
$testimonial_labels = array(
'name' => __( 'Testimonial', 'post type general name', 'jmr-cpt' ),
'singular_name' => __( 'Testimonial', 'post type singular name', 'jmr-cpt' ),
'add_new' => __( 'Add New', 'testimonial', 'jmr-cpt' ),
'add_new_item' => __( 'Add New Testimonial', 'jmr-cpt' ),
'edit_item' => __( 'Edit Testimonial', 'jmr-cpt' ),
'new_item' => __( 'New Testimonial', 'jmr-cpt' ),
'all_items' => __( 'All Testimonials', 'jmr-cpt' ),
'view_item' => __( 'View Testimonials', 'jmr-cpt' ),
'search_items' => __( 'Search Portfolio', 'jmr-cpt' ),
'not_found' => __( 'No testimonials found','jmr-cpt' ),
'not_found_in_trash' => __( 'No testimonials found in the Trash', 'jmr-cpt' ),
'parent_item_colon' => '',
'menu_name' => 'Testimonials'
);
$testimonial_args = array(
'labels' => $testimonial_labels,
'description' => __('Contains testimonials', 'jmr-cpt'),
'exclude_from_search' => true,
'public' => true,
'show_ui' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-smiley',
'supports' => array( 'title', 'editor', 'excerpt' ),
'rewrite' => array(
'slug' => 'testimonial',
'with_front' => true,
'feeds' => true,
'pages' => true
),
'has_archive' => false,
'show_in_rest' => true
);
register_post_type( 'testimonial', $testimonial_args );
}
add_action( 'init', 'jmr_custom_posts' );
// ************* FLUSH URL REWRITE RULES ***************************** //
function rmd_rewrite_flush() {
rmd_custom_posts();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'jmr_rewrite_flush' );
@jamesmrobinson
Copy link
Author

This code is adapted from in a tutorial in Smashing Magazine by Daniel Pataki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment