Categories

Single Category Permalink

Author: Scott Reilly
Version: 2.5.2
First released: 2011-07-18
Last update: 2021-09-29
Compatibility: WP 4.6 – 5.8.9
Download: [ zip ]
Description:

Reduce permalinks (category or post) that include entire hierarchy of categories to just having the lowest level category.

Extended Description

Reduce permalinks (category or post) that include entire hierarchy of categories to just having the lowest category in the hierarchy.

By default, WordPress replaces the %category% permalink tag in a custom permalink structure with the entire hierarchy of categories for the post’s first matching category. For example, assuming your site has a hierarchical category structure like so:

Applications
  |_ Desktop
  |_ Web
    |_ WordPress

By default, if you have a permalink structure defined as %category%/%year%/%monthnum%/%day%/%postname%, your post titled “Best Plugins” assigned to the “WordPress” category would have a permalink of:

https://www.example.com/applications/web/wordpress/2008/01/15/best-plugins

If you activate the Single Category Permalink plugin, this would be the permalink generated for the post (and recognized by the blog):

https://www.example.com/wordpress/2008/01/15/best-plugins

In order for a category to be used as part of a post’s permalink structure, %category% must be explicitly defined in the Settings -> Permalinks admin page as part of a custom structure, i.e. /%category%/%postname%.

For category links, %category% is implied to follow the value set as the “Category base” (or the default category base if none is specified). So if your category base is ‘category’, the above example would list posts in the ‘WordPress’ category on this category listing page:

https://www.example.com/category/applications/web/wordpress/

With this plugin activated, that link would become:

https://www.example.com/category/wordpress/

NOTE: The fully hierarchical category and post permalinks will continue to work. The plugin issues a 302 redirect to browsers and search engines pointing them to the shorter URL.

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Hooks

The plugin exposes one filter for hooking. Code using this filter should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Less ideally, you could put them in your active theme’s functions.php file.

c2c_single_category_redirect_status (filter)

The ‘c2c_single_category_redirect_status’ hook allows you to specify an HTTP status code used for the redirect. By default this is 302.

Arguments:

  • $status (integer) : The default HTTP status code

Example:

/**
 * Change the redirection HTTP status to a 302.
 *
 * @param  int $code The HTTP status code. By default 301.
 * @return int
 */
function scp_change_redirect_status( $code ) {
    return 302;
}
add_filter( 'c2c_single_category_redirect_status', 'scp_change_redirect_status' );

Find out more at the plugin’s WordPress Plugin Repository page.

Installation

  1. Install via the built-in WordPress plugin installer. Or download and unzip single-category-permalinks.zip inside the plugins directory for your site (typically wp-content/plugins/)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. Use %category% as a permalink tag in the Settings -> Permalinks admin options page when defining a custom permalink structure

Release Log

2.5.2 (2021-09-28)

  • Change: Note compatibility through WP 5.8+
  • Unit tests:
    • Change: Restructure unit test directories
      • Change: Move phpunit/bin/ into tests/
      • Change: Move phpunit/ into tests/
    • Change: Remove ‘test-‘ prefix from unit test file
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

2.5.1 (2021-04-30)

  • Change: Note compatibility through WP 5.7+
  • Change: Update copyright date (2021)

2.5 (2020-09-23)

  • Fix: Default the $taxonomy argument of category_link() to ‘category’ to avoid a PHP warning/error
  • Fix: Handle the possibility that get_category() could return null for an invalid category ID
  • New: Add a TODO item about removing deprecated functions (which is not something I want to do just yet, hence the TODO)
  • Change: Update docs for return value of category_link() to reflect that WP_Error or null are also possible values
  • Change: Note compatibility through WP 5.5+
  • Change: Restructure unit test file structure
    • New: Create new subdirectory phpunit/ to house all files related to unit testing
    • Change: Move bin/ to phpunit/bin/
    • Change: Move tests/bootstrap.php to phpunit/
    • Change: Move tests/ to phpunit/tests/
    • Change: Rename phpunit.xml to phpunit.xml.dist per best practices
  • Unit tests:
    • New: Add tests for category_link(), post_link()
    • New: Add unset_permalink_structures() to unset configured permalink structures

Copyright & Disclaimer

Copyright © 2011-2024 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.

2 replies on “Single Category Permalink”

Leave a Reply

Your email address will not be published.