Skip to content

Instantly share code, notes, and snippets.

@shaunpalmer
Created June 29, 2021 10:49
Show Gist options
  • Save shaunpalmer/378df8ac3a9bab7a55269b5dbe35fb30 to your computer and use it in GitHub Desktop.
Save shaunpalmer/378df8ac3a9bab7a55269b5dbe35fb30 to your computer and use it in GitHub Desktop.
tutorial Custom Post Type
<?php
/*
* Plugin Name: tutorial
Plugin URI:https://shaunpalmer.co.nz
Description: All tutorial Custom Post Type & taxonomies that adds custom post types
Version: 0.1.6
Author: shaun palmer
Author URI: [email protected]
Text Domain: sp-ps-td-shaun-palmer
*Tested up to: 5.7
*/
//=================================================
// Security: Abort if this file is called directly
//=================================================
if ( ! defined( 'WPINC' ) ) {
die;
}
//=================================================
// Security: Exit if accessed directly if this file is called directly
//=================================================
if(!defined('ABSPATH')){
exit; // Exit if accessed directly
return;
}
// Register Custom Post Type tutorial
function create_tutorial_cpt() {
$labels = array(
'name' => _x( 'tutorials', 'Post Type General Name', 'sp-ps-td-shaun-palmer' ),
'singular_name' => _x( 'tutorial', 'Post Type Singular Name', 'sp-ps-td-shaun-palmer' ),
'menu_name' => _x( 'tutorials', 'Admin Menu text', 'sp-ps-td-shaun-palmer' ),
'name_admin_bar' => _x( 'tutorial', 'Add New on Toolbar', 'sp-ps-td-shaun-palmer' ),
'archives' => __( 'tutorial Archives', 'sp-ps-td-shaun-palmer' ),
'attributes' => __( 'tutorial Attributes', 'sp-ps-td-shaun-palmer' ),
'parent_item_colon' => __( 'Parent tutorial:', 'sp-ps-td-shaun-palmer' ),
'all_items' => __( 'All tutorials', 'sp-ps-td-shaun-palmer' ),
'add_new_item' => __( 'Add New tutorial', 'sp-ps-td-shaun-palmer' ),
'add_new' => __( 'Add New', 'sp-ps-td-shaun-palmer' ),
'new_item' => __( 'New tutorial', 'sp-ps-td-shaun-palmer' ),
'edit_item' => __( 'Edit tutorial', 'sp-ps-td-shaun-palmer' ),
'update_item' => __( 'Update tutorial', 'sp-ps-td-shaun-palmer' ),
'view_item' => __( 'View tutorial', 'sp-ps-td-shaun-palmer' ),
'view_items' => __( 'View tutorials', 'sp-ps-td-shaun-palmer' ),
'search_items' => __( 'Search tutorial', 'sp-ps-td-shaun-palmer' ),
'not_found' => __( 'Not found', 'sp-ps-td-shaun-palmer' ),
'not_found_in_trash' => __( 'Not found in Trash', 'sp-ps-td-shaun-palmer' ),
'featured_image' => __( 'Featured Image', 'sp-ps-td-shaun-palmer' ),
'set_featured_image' => __( 'Set featured image', 'sp-ps-td-shaun-palmer' ),
'remove_featured_image' => __( 'Remove featured image', 'sp-ps-td-shaun-palmer' ),
'use_featured_image' => __( 'Use as featured image', 'sp-ps-td-shaun-palmer' ),
'insert_into_item' => __( 'Insert into tutorial', 'sp-ps-td-shaun-palmer' ),
'uploaded_to_this_item' => __( 'Uploaded to this tutorial', 'sp-ps-td-shaun-palmer' ),
'items_list' => __( 'tutorials list', 'sp-ps-td-shaun-palmer' ),
'items_list_navigation' => __( 'tutorials list navigation', 'sp-ps-td-shaun-palmer' ),
'filter_items_list' => __( 'Filter tutorials list', 'sp-ps-td-shaun-palmer' ),
);
$args = array(
'label' => __( 'tutorial', 'sp-ps-td-shaun-palmer' ),
'description' => __( ' All tutorial Custom Post Type & taxonomies that adds custom post types', 'sp-ps-td-shaun-palmer' ),
'labels' => $labels,
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'author', 'page-attributes', 'post-formats', 'custom-fields'),
'taxonomies' => array('tutorial'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 10,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'hierarchical' => true,
'exclude_from_search' => false,
'show_in_rest' => true,
'rest_base' => 'tutorial',
'query_var' => 'tutorial',
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'tutorial', $args );
}
add_action( 'init', 'create_tutorial_cpt', 0 );
//For the theme: Flush rewrite rules to add "Services" as a permalink slug
function deactivate_SP_tu__theme( $new_theme ) {
flush_rewrite_rules(false);
}
add_action( 'switch_theme', 'deactivate_SP_PS__theme' );
function SP_tu_flush_rewrite_rules($new_theme){
// force rewrite rules to be recreated at the right time
//flush_rewrite_rules();
delete_option( 'rewrite_rules');
//flush_rewrite_rules();
}
do_action( 'after_switch_theme', 'SP_tu_flush_rewrite_rules' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment