Taxonomic SEO Permalinks

Custom WordPress Taxonomy feature was introduced hoping that it will help users create some kind of groups just like regular WordPress category or tags. Custom taxonomies gives control over personalizing the blog to great extend. Now users can create meaningful taxonomies that suits the subject.

When I tried to create projects by using the taxonomies, I just had a problem, and that was with using taxonomies in URLs. I found that WordPress does support the feature that helps in custom taxonomies in queries, but as I tried and tested, I couldn’t use them in Permalink settings just like we use /%category%/%postname%/. So I created this plugin.

You can download Taxonomic SEO Permalinks plugin from WordPress.org website.

TSP Version 0.3.1

Now supports sub-taxonomy in URLs. People requested me to support sub-taxonomy just like WordPress supports sub-category in URLs. For example if you use %category% in permalink structure and a post has sub-category, %category% will be replaced with ‘parent-category-slug/child-category-slug’.

In order to do similar things with custom taxonomies I tried many things but the solutions was getting complicated just because of forward slash (/) character as separator. So in order to keep things as simple as possible I used plus (+) character to separate child and parent custom taxonomies. I know this is not what most of us want but still it solves sub-taxonomy problem. I will fix this as soon as possible.

Also I used a limit of three parts per replacement. So a custom taxonomy structure tag can be replaced with ‘grandparent-slug+parent-slug+child-slug’ at the max and won’t show ‘great-grandparent-slug’ even if present.

TSP Version 0.2.0

Few major changes done on programming side. The plugin is made more general and should be working without any manual changes. No need to edit plugin file, almost everything is done automatically.

TSP Version 0.1.3 and Earlier

This version was written to work on both standalone blog as well as network blog. Also taxonomies needed to be edited manually inside plugin.

I am a developer from India. Visit my Google+ profile for more information:

Posted in Projects Tagged with: , ,
21 comments on “Taxonomic SEO Permalinks
  1. Hello.

    It seems that class.tsp.php file is missing in zip file.

    Have a good day.

  2. XU3E says:

    Och man, you saved my life! I was really upset of this issue… after hours research i found this! Really and deeply thank you…

  3. Ferux says:

    Hi, will it work with post types too?

    Thanks

  4. Patrick says:

    Hi, nice plugin — I edited it so if a taxonomy value doesn’t exist, it doesn’t return anything and configured the following:

    My permalink setup: /%category%/%postname%%taxonomyname%/
    In plugin code (line 99): $permalink = str_replace($tax_token, ‘-’.$long_slug, $permalink);
    (I added a leading “-”)

    Desired URLs:
    IF taxonomy exists: site.com/category/postname-taxonomy/
    IF taxonomy doesn’t exist: site.com/category/postname/

    Here are the resulting post URLs:
    IF taxonomy exists: site.com/category/postname-taxonomy/ (as intended, but leads to a 404 page)
    IF taxonomy doesn’t exist: site.com/category/postname-/ (don’t want the trailing “-”)

    Is there a way I can get the desired URLs and not resolve a 404?

    Thanks for your help.

    • Patrick, WordPress reads the URL entered and it divides it into pieces according to rules (WP Rewrite) defined with the help of regular expressions. It treats ‘/’ (forward slash) as a separator and it’s difficult to understand for WordPress when we use ‘-’ (dash / hyphen) as separator, which is also used as a replacement for space. So when URL contains something like “…/my-real-post-name/” which is actually replacement for %postname%, it is divided (sliced) into a piece and compared with post name slugs in database. Problem with URLs like site.com/category/postname-taxonomy/ is that regular expressions will compare /postname-taxonomy/ part with post name slugs in the database, causing 404 page.

      I think, we cannot skip one of the pieces like category or custom taxonomy if you have mentioned it in permalink structure. That’s why when we don’t specify a category to a post, ‘uncategorized’ appears in the post URL. This plugin replaces ‘no-taxonomy’ if taxonomy is not specified for the post. However, there might be a fix around. If you find it, let me know.

  5. Patrick says:

    Hi Rakesh, thanks for the reply. What’s odd is site.com/category/postname–taxonomy/ (with 2 hyphens) does resolve properly. Still working on hiding the trailing “-” however.

    Related, if I went with the following URL:
    site.com/category/postname/taxonomy1/taxonomy2/

    …if both taxonomy fields are null (blank), how could I prevent the following URL?:
    site.com/category/postname/// (note the extra slashes)

    Thanks again.

    • You will need to tweak functions for doing these. You can either give taxonomy a default value inside functions just like ‘uncategorized’ or you can tweak the part of functions where %taxonomy1% is being replaced. Remember to add WP Rewrite rules to accept your ‘site.com/category/postname’ URL.

  6. End User says:

    Starting the permalink structure with a custom taxonomy works for posts, however it breaks WP page URLs. site.com/%taxonomy%/%category%/%post-name%/

    If I move the custom taxonomy in the permalink structure to not start at the begining of the URL, it works for WP pages. Example: site.com/%category%/%taxonomy%/%post-name%/

    Is there a work around?

  7. Hi Rakesh,

    As I understood, it does not support taxonomies of custom post types. If so, could you please instruct me briefly how can I implement this to my theme-posttypes.php?

    • Hello,
      If you read the code, it is just a two step hack. First, looping through custom taxonomies, add rewrite tags

      add_rewrite_tag($tax_token, ‘(.+)’, $tax_name.’=’);

      Second, it reads the permalink structure set by user, and for each post it replaces %my_taxonomy_token% with value set in the post for that taxonomy.

      For each of these step there is a separate function. If you can use the same logic to iterate through custom post types, you might get desired output. I will look into this as soon as possible, and if you solve the problem before I do, let me know.

  8. Jochen says:

    Hey Rakesh,

    thanks for this great plugin. I’ve been using it for a coulpe of days now, but i’m runnng into _one_ single problem.

    my permalinks look like this:
    /%movies%/%category%/%postname%/

    if a post is /themovie/category-one/postname/ it works. but it doesn’t with sub-categories, for example: /themovie/category-one/sub-category-one/postname/

    this gives me an 404 error page.
    do you have a trick that might work? :)

  9. Jochen says:

    Little correction:
    /themovie/category-one/ works
    /themovie/category-one/sub_category/ doesn’t
    /themovie/category-one/sub_category/postname/ works

    any ideas? :)

  10. Thank you very much!! You saved me from a sure headache!

  11. smuda says:

    Hey rakesh,

    is there a way to make your plugin work with sub-categries?
    my current structure is this:
    /%thetag%/category/subcategory/post/

    what’s weird:
    /%thetag%/category/ works!
    /%thetag%/category/subcategory/ doesn’t work
    /%thetag%/category/subcategory/post works!

    do you have any idea, what might cause it? or might fix it?

    regards,
    alex

    • It does not work on any other term. It simply just replaces %thetag% with the text for that taxonomy… When url entered, it just ignores taxonomy, and take other parts of url for getting post / category information.

      • smuda says:

        hey rakesh,
        thx for your quick reply.

        that’s my problem, it’s doesn’t get all other information. my permalink-settings look like this:
        /%thetag%/%category%/%postname%/

        if subcategories are entered, it should contain them in the url, right?
        currently it gives me a 404.

  12. Binod says:

    This works fine… But im getting 404 errors while i try to view the pages. Any changes that we can make to correct this issue?

    Please let me know

    Thanks,
    Binod

Leave a Reply