Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress show taxonomy under custom admin menu

Tags:

php

wordpress

I'm trying to show a custom taxonomy under a admin menu item which is just a page i.e. http://example.com/wp-admin/admin.php?page=bla.

According to the WordPress Dev. page for show_in_menu under register_taxonomy it says the following:

'some string' - If an existing top level page such as 'tools.php' or 'edit.php?post_type=page', the post type will be placed as a sub menu of that.

Does this mean taxonomies cannot be shown under anything but those?

PHP

<?php
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Genres', 'taxonomy general name' ),
        'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Genres' ),
        'all_items'         => __( 'All Genres' ),
        'parent_item'       => __( 'Parent Genre' ),
        'parent_item_colon' => __( 'Parent Genre:' ),
        'edit_item'         => __( 'Edit Genre' ),
        'update_item'       => __( 'Update Genre' ),
        'add_new_item'      => __( 'Add New Genre' ),
        'new_item_name'     => __( 'New Genre Name' ),
        'menu_name'         => __( 'Genre' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_in_menu'      => 'bla', // not working | tried admin.php?page=bla as well, also not working
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
    );

    register_taxonomy( 'genre', array( 'book' ), $args );
}
like image 339
adamj Avatar asked Dec 05 '22 20:12

adamj


2 Answers

I've found a way around this problem, code below:

    add_action( 'admin_menu', 'shmeh_menu' );
    add_action( 'parent_file', 'menu_highlight' );

    function shmeh_menu() {
        add_submenu_page( 'bla', 'Shmeh', 'Shmeh', 'manage_options', 'edit-tags.php?taxonomy=shmeh');
    }

    function menu_highlight( $parent_file ) {
        global $current_screen;

        $taxonomy = $current_screen->taxonomy;
        if ( $taxonomy == 'shmeh' ) {
            $parent_file = 'bla';
        }

        return $parent_file;
    }
like image 91
adamj Avatar answered Dec 09 '22 16:12

adamj


In your Taxonomy registration:

show_in_menu => true

You then need to add a submenu page:

$parent_slug = 'slug_for_parent_menu';
$page_title = 'Submenu Page Title';
$menu_title = 'Submenu Page Title';
$capability = 'manage_options';
$menu_slug = 'edit-tags.php?taxonomy=bla&post_type=custom_post_type_name';
$function = null;
$position = null;
add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function, $position);

finally:

add_filter('parent_file', 'menu_highlight');    
function menu_highlight($parent_file) {
    global $plugin_page, $submenu_file, $post_type, $taxonomy;
    if ('custom_post_type_name' == $post_type) {
        if ($taxonomy == 'bla') {
            $plugin_page = 'edit-tags.php?taxonomy=bla&post_type= custom_post_type_name'; // the submenu slug 
            $submenu_file = 'edit-tags.php?taxonomy=bla&post_type= custom_post_type_name';    // the submenu slug
        }
    } 
    return $parent_file;
}

Hope I put all that in correctly. Nest any else if's inside the taxonomy check and add same for other post types.

like image 22
Reinaldo Novoa Avatar answered Dec 09 '22 15:12

Reinaldo Novoa