Skip to content

Instantly share code, notes, and snippets.

@andreawetzel
Last active February 10, 2022 02:34
Show Gist options
  • Save andreawetzel/c2a0da4ba7f798e31030b8d5f2983509 to your computer and use it in GitHub Desktop.
Save andreawetzel/c2a0da4ba7f798e31030b8d5f2983509 to your computer and use it in GitHub Desktop.
CPT Plugin
<?php
/*
Plugin Name: WI Verbs Nested Post Types
Plugin URI: https://wisconsinverbs.com
Description: Display Custom Post Types in a nested menu
Version: 1.0
Author: Andrea Roenning
Author URI: https://andrearoenning.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wiverb
*/
if(!function_exists('wiverb_post_types')) {
/**
* Adds custom post types
*/
function wiverb_post_types() {
global $wp_rewrite;
/*
* Hiking Trail listing
*/
register_post_type( 'wiverb_hike',
array(
'labels' => array(
'name' => __( 'Hiking Trails' ),
'singular_name' => __( 'Hiking Trail' ),
'add_new_item' => __('Add New Trail'),
'edit_item' => __('Edit Trail'),
'new_item' => __('New trail'),
'view_item' => __('View trail'),
'search_items' => __('Search trails'),
'not_found' => __('No trails were found'),
'not_found_in_trash' => ('No trails found in trash')
),
'public' => true,
'show_in_menu' => false,
'exclude_from_search' => true,
'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes'),
'rewrite' => array( 'slug' => 'trails/hiking' ),
'has_archive' => 'trails/hiking'
)
);
/*
* Biking Trail listing
*/
register_post_type( 'wiverb_bike',
array(
'labels' => array(
'name' => __( 'Biking Trails' ),
'singular_name' => __( 'Biking Trail' ),
'add_new_item' => __('Add New Trail'),
'edit_item' => __('Edit Trail'),
'new_item' => __('New trail'),
'view_item' => __('View trail'),
'search_items' => __('Search trails'),
'not_found' => __('No trails were found'),
'not_found_in_trash' => ('No trails found in trash')
),
'public' => true,
'show_in_menu' => false,
'exclude_from_search' => true,
'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes'),
'rewrite' => array( 'slug' => 'trails/biking' ),
'has_archive' => 'trails/biking'
)
);
}
add_action( 'init', 'wiverb_post_types' );
}
/*
* Admin Controls
*/
if(!function_exists('wiverbs_trails_menu')) {
/**
* Adds admin nav & subnav
*/
function wiverbs_trails_menu() {
add_menu_page( 'Trails', 'Trails', 'edit_posts', 'wiverbs_trails', 'wiverbs_trails_page', 'dashicons-location-alt', 5 );
add_submenu_page('wiverbs_trails', 'Hiking Trails', 'Hiking Trails', 'edit_posts', 'edit.php?post_type=wiverb_hike');
add_submenu_page('wiverbs_trails', 'Biking Trails', 'Biking Trails', 'edit_posts', 'edit.php?post_type=wiverb_bike');
}
add_action( 'admin_menu', 'wiverbs_trails_menu' );
}
if(!function_exists('wiverbs_trails_page')) {
/**
* Add content to the admin trails top-level page
*/
function wiverbs_trails_page() {
if ( !current_user_can( 'edit_posts' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<h2>Trails</h2><p>Edit trails by selecting from the list below';
echo '<ul>';
echo '<li><a href="'. home_url() .'/wp-admin/edit.php?post_type=wiverb_hike">Hiking Trails</a></li>';
echo '<li><a href="'. home_url() .'/wp-admin/edit.php?post_type=wiverb_bike">Biking Trails</a></li>';
echo '</ul>';
echo '</div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment