Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Created February 28, 2020 17:31
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 MaximeCulea/7be822f48fed011561c65716d8114990 to your computer and use it in GitHub Desktop.
Save MaximeCulea/7be822f48fed011561c65716d8114990 to your computer and use it in GitHub Desktop.
Create a custom admin menu in order to attach a post type and taxonomy.
<?php class Custom_Admin_Menu {
/**
* I have an e-sport section with is related to a program and players (as post types)
* Also related to theses, we specify scenes and games (as taxonomies)
* In fine the idea is to gather all this into a single admin menu :
* E-Sport
* |-- Programs
* |-- Players
* |-- Scenes
* |-- Games
*/
function __hooks() {
// Go to the post types and edit the "show_in_menu" => "e-sport"
add_action( 'admin_menu', [ $this, 'custom_e_sport_admin_menu' ] );
add_action( 'admin_menu', [ $this, 'taxonomy_admin_menu' ] );
add_action( 'parent_file', [ $this, 'post_type_parent_file' ] );
// You could replicate as much post type and taxonomies you need
}
/**
* Register a custom e-sport admin menu page
*/
function custom_e_sport_admin_menu() {
add_menu_page( 'E-Sport', 'E-Sport', 'manage_options', 'e-sport', '', 'dashicons-nametag', 10 );
}
/**
* Build the custom taxonomy admin submenu
*/
function taxonomy_admin_menu() {
// Care about the encoded url, because it will match the edit screen for the "active" class
add_submenu_page( 'e-sport', 'Games', 'Games', 'manage_options', 'edit-tags.php?taxonomy=game&amp;post_type=player' );
}
/**
* Ensure the "active" parent for the post type as admin submenu
*
* @param $parent_file
*
* @return string
*/
function post_type_parent_file( $parent_file ) {
global $post_type;
if ( 'player' == $post_type ) {
$parent_file = 'e-sport';
}
return $parent_file;
}
}
$cam = new Custom_Admin_Menu();
$cam->__hooks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment