Tag

actions

How To Change Your WordPress Category, Tag, or Post Format Permalink Structure

By | Tutorial | 3 Comments

There are five default WordPress taxonomies: category, post_tag, post_format, nav_menu, and link_category – but nav_menu and link_category are off in a world of their own so let’s pretend they don’t exist for now. All of the settings for these taxonomies are preset by the fine folks at WordPress, and hard-coded into WordPress core, so if you want to make any changes, you have to do a little work.

The category, post_tag, and post_format default slugs, or permalinks, are simple. Categories are preceded by the slug ‘category’, post tags are preceded by the slug ‘tag’, and post formats are preceded by the slug ‘type’. In other words…

But what if you wanted to change the slug to something else? Or get rid of it altogether?

Changing the Slug

Changing the category/tag slug is pretty simple. The base for the slugs are stored as options so all you have to do is hook into the ‘pre_option_{option name}’ filter that’s applied when options are retrieved, allowing you to change the value that’s returned. The category slug’s option name is ‘category_base’ and the tag slug’s option name is ‘tag_base’ so the filters are ‘pre_option_category_base’ and ‘pre_option_tag_base’.

Changing the post format slug is a little different. The base is not stored as a option but it does have a filter: ‘post_format_rewrite_base’.

The following code will, most likely, go in your functions.php file. Remember that filters must always return a value. Learn more about filters.

add_filter( 'pre_option_category_base', 'mysite_change_category_base' );
function mysite_change_category_base( $value ) {
   
   // let's change our category slug to rantings
   // this will change the permalink to http://wpdreamer.com/rantings/wordpress/
   return 'rantings';

}

add_filter( 'pre_option_tag_base', 'mysite_change_tag_base' );
function mysite_change_tag_base( $value ) {
   
   // let's change our tag slug to ravings
   // this will change the permalink to http://wpdreamer.com/ravings/custom-post-types/
   return 'ravings';

}
add_filter( 'post_format_rewrite_base', 'mysite_change_post_format_base' );
function mysite_change_post_format_base( $value ) {
	
   // let's change our tag slug to fanciness
   // this will change the permalink to http://wpdreamer.com/fanciness/video/
   return 'fanciness';

}

Removing The Slug

Removing the slug altogether requires a different workaround, but it’s still pretty simple.

When WordPress registers these default taxonomies, the register_taxonomy() function adds a permastruct for each taxonomy that registers its permalink structure. All you need to do is overwrite the permastruct with your own.

These taxonomies and permastructs are registered during the ‘init’ action on priority level 0 (highest priority) so we’ll add our own hook into the ‘init’ action, but give it a lower priority so it’s executed after WordPress does its thing.

The following code will, most likely, go in your functions.php file. Learn more about actions.

Heads up: I don’t recommend removing the slug from categories, tags AND post formats. In fact, I wouldn’t recommend removing the slug from more than one of these taxonomies because then WordPress would have a very difficult time figuring out your URLs. The code for removing all three is only shown together so you can pick which set you need.

If you still want to remove multiple slugs, or if you’re having issues with your changes, I recommend installing the Rewrite Rules Inspector plugin. It shows you all of your rewrites and allows you to filter out which rewrites match a specific URL.


// make sure our action's priority is lower than 0,
// which is the highest priority, so basically any
// number greater than 0 will do. let's go with 1.
add_action( 'init', 'mysite_change_taxonomy_permalinks', 1 );
function mysite_change_taxonomy_permalinks() {

   // changing the category permastruct
   $taxonomy = 'category';
		
   // change the settings at will but make sure 'slug' is set to NULL
   $category_rewrite = array(
      'hierarchical' => true, // categories are defaulty hierarchical
      'slug' => NULL, // we don't want no slug!
      'with_front' => false, // this is up to you
      'ep_mask' => EP_CATEGORIES // this is a constant set by WordPress we'll use
      );
			
   // overwrites default category permastruct
   add_permastruct( $taxonomy, "%$taxonomy%", $category_rewrite );
		
   // ----------------------------------
		
   // changing the post_tag permastruct
   $taxonomy = 'post_tag';
		
   // change the settings at will but make sure 'slug' is set to NULL
   $post_tag_rewrite = array(
      'slug' => NULL, // we don't want no slug!
      'with_front' => false, // this is up to you
      'ep_mask' => EP_TAGS // this is a constant set by WordPress we'll use
      );
			
   // overwrites default post_tag permastruct
   add_permastruct( $taxonomy, "%$taxonomy%", $post_tag_rewrite );
			
   // ----------------------------------
		
   // changing the post_format permastruct
   $taxonomy = 'post_format';
		
   // change the settings at will but make sure 'slug' is set to NULL
   $post_format_rewrite = array(
      'slug' => NULL, // we don't want no slug!
      );
			
   // overwrites default post_format permastruct
   add_permastruct( $taxonomy, "%$taxonomy%", $post_format_rewrite );

}

Heads up: Sometimes messing with permalink settings requires you flush your system’s rewrite rules in order for the changes to take place. But they only need to be flushed once, each time you make a change, and not every time a page loads. To flush your rules, go to Settings -> Permalinks and click “Save Changes”.

How to Hide the WordPress Admin Bar

By | Tutorial, WordPress | No Comments

The WordPress admin bar, or toolbar, can be really helpful but sometimes it gets in my way, especially when I’m trying a new design and having difficulty imagining what it looks like without a dark gray bar running across the top.

For most people, with a single WordPress install, the solution is simply a matter of going into your profile and unchecking “Show Toolbar when viewing site”. But what if you’re running WordPress multisite? Unchecking that box removes the toolbar across your entire network when you only wanted to hide the toolbar on the new site you’re designing.

Sometimes I require a password for front-end content but there’s no need to grant access to the admin/dashboard, or frankly for them to even know it exists, so I’d rather not confuse them with a pointless toolbar.

Thankfully, as with most functionality in WordPress, there’s a pretty easy way to tell the admin bar to go away.

  1. Write a function which hooks into the “after_setup_theme” action.
    • Hooking into this action is important because it allows us to also remove the styles and scripts that WordPress enqueues for the admin bar. If they are not removed, then there will still be a space at the top of your page for the bar and that’s not very helpful.
  2. Inside your function, call the WordPress function, show_admin_bar() and pass just one parameter: false

There’s obviously a wide variety of scenarios for when and where to hide your admin bar, but hopefully the following examples will give you a good groundwork. This code will, most likely, go in your functions.php file.

Always Hide The Admin Bar

If you want to hide the admin bar for good, no matter who’s looking at the site, the code is pretty simple.

add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {
   show_admin_bar( false );
}

Hide the Admin Bar If The User Doesn’t Have Access To The Admin/Dashboard

If you want to hide the admin bar from users who don’t have access to the admin/dashboard, then check their user capabilities. The capability to “read” is what allows access to the administration panel.

add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {
   
   // if the user cannot "read", then they cannot access the admin/dashboard
   if ( ! current_user_can( 'read' ) )
      show_admin_bar( false );

}

Hide The Admin Bar on Specific Multisite Sites

If you want to hide the admin bar on specific sites on your multisite network, use the global $blog_id variable in your logic. The global $blog_id variable contains the ID of which site is currently being viewed.

add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {
   
   // access the global variable
   global $blog_id;

   // remove the admin bar for one site
   if ( 3 == $blog_id )
      show_admin_bar( false );

   // remove the admin bar from multiple sites
   if ( in_array( $blog_id, array( 3, 5, 8, 10 ) ) )
      show_admin_bar( false );

}

Hide The Admin Bar on Specific Pages

Using WordPress conditional tags allows you to easily hide the admin bar on specific sections, or pages, of your site.

add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {

   // hide the admin bar on your main page
   if ( is_home() )
      show_admin_bar( false );

   // hide the admin bar on your page with the title of 'About Me'
   if ( is_page( 'About Me' )
      show_admin_bar( false );

   // hide the admin bar on any taxonomy archive page
   if ( is_tax() )
      show_admin_bar( false );

}