Skip to content

Instantly share code, notes, and snippets.

@diggy
Created April 25, 2012 15:15
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 diggy/2490532 to your computer and use it in GitHub Desktop.
Save diggy/2490532 to your computer and use it in GitHub Desktop.
Add taxonomy navigation to the WordPress admin toolbar
<?php
add_action( 'admin_bar_menu', 'pjh_add_taxonomies_to_toolbar', 999 );
function pjh_add_taxonomies_to_toolbar( $wp_admin_bar )
{
// check permissions
if( ! current_user_can( 'manage_categories' ) ) return;
global $wp_taxonomies;
// for each taxonomy
foreach( $wp_taxonomies as $taxonomy => $tax_data )
{
// skip non-hierarchical taxonomies
if( ! is_taxonomy_hierarchical( $taxonomy ) ) continue;
// skip specific taxonomies
// if( in_array( $taxonomy, array( 'genre', 'country' ) ) ) continue;
$tax = get_taxonomy( $taxonomy );
$url = admin_url( '/edit-tags.php?taxonomy=' . $tax->name );
// get terms
$tax_terms = get_terms( $taxonomy, 'number=9999&orderby=count&order=DESC&hide_empty=1' );
// limit number of terms added to toolbar
// $tax_terms = array_slice( $tax_terms, 0, 10 );
// add parent nodes to toolbar
$wp_admin_bar->add_node( array(
'id' => $tax->name,
'title' => $tax->labels->name,
'href' => $url,
'meta' => array(
'class' => $tax->name . '-toolbar-page',
'title' => $tax->labels->name
),
'parent' => false
) );
// for each term in taxonomy
foreach( $tax_terms as $array => $data )
{
// determine parent and set up variables
if( $data->parent != 0 ) :
$term = get_term( $data->term_id, $taxonomy );
$parent_term = get_term( $data->parent, $taxonomy );
$parent = $parent_term->name;
$link = get_term_link( $term );
else :
$parent = $tax->name;
$term = get_term( $data->term_id, $tax->name );
$link = get_term_link( $term );
endif;
$name = esc_attr( $data->name );
$count = $data->count;
// add additional query arguments
// $query_arg = array( 'foo' => 'bar', 'baz' => 'qux' );
// add subitems to parents
$wp_admin_bar->add_node( array(
'id' => $name,
'title' => $name . ' (' . $count . ')',
'href' => $link,
//'href' => add_query_arg( $query_arg, untrailingslashit( $link ) ), // add additional query arguments
'meta' => array(
'class' => $name . '-toolbar-page',
'title' => $data->description
),
'parent' => $parent
) );
}
}
}
@diggy
Copy link
Author

diggy commented Apr 25, 2012

Paste the snippet in the functions.php file of your theme and adjust to your liking.

Notes

  • Toolbar nodes will only be added if the current user can manage categories. Comment out line 6 (and take care of line 20) to enable for all logged in users.
  • Only hierarchical taxonomies (categories, …) will be added to the toolbar. Comment out line 14 if you wish to display non-hierarchical taxonomies (post tags, …) as well.
  • To exclude specific taxonomies uncomment and configure line 17.
  • Terms are ordered by post count, and empty taxonomy terms (post count 0) are not shown. To return all items, change the value of hide_empty on line 23 to 0.
  • Number of items shown is unlimited. You can limit the number of items that will be added to the toolbar with array_slice on line 26.
  • If you would like to add extra query arguments, take a look at lines 59 and 66.
  • This snippet requires WordPress 3.3 or higher

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