Wordpress Plugins: sCategory Permalink - select category for permalink generation

Nov 18
2006 11:12 (Development, WordPress) · Русский (17,883 views)

I’m starting publishing custom Wordpress plugins used in this blog, and today it will be “sCategory Permalink”. I like permalink option of Wordpress %category% (and using it here), but it has one great limitation - when this option is selected, Wordpress uses category with lowest ID for permalink generation! Just imagine following scenario: you have category “Development” (common for all posts related to software building), and several specific categories, for example “PHP”, “AJAX”, “JavaScript”. You have Development category ID greater than any of other categories IDs, therefor specific categories used for URL generation. But one day you decided to start learning Ruby on Rails and post about this in your blog. It’s the problem, because when you will create category “Ruby on Rails”, its ID will be greater then ID of “Development”. Now you have to take decision: to abandon posting in both categories at the same time, or to update “Development” category ID. Lastest can be done in two ways: remove category from Wordpress administration area and re-add it (and then you need to go through all of your posts and add needed to this category), or update ID in database (there are several tables related on category ID). But now you can relax! Just download and install plugin!

Installation

  1. Download and unpack plugin files to wp-content/plugins/scategory_permalink directory.
  2. Enable “sCategory Permalink” plugin on your Plugins page in Site Admin.
  3. Go to the Options/Permalinks page in Site Admin and use %scategory% option in Custom text field (you can look here for other options). In this blog I’m using /%scategory%/%postname%/.
  4. Now on Write Post page near the categories checkboxes radio button will appear:
    sCategory Permalink
    Select radio button near category which will be used in permalink.
  5. Have fun!

Download

You could always download latest version of the plugin here.

16 Responses to 'Wordpress Plugins: sCategory Permalink - select category for permalink generation'

Subscribe to comments with RSS or TrackBack to 'Wordpress Plugins: sCategory Permalink - select category for permalink generation'.

1
said on 2007-02-08 at 12.26 pm

Amazing plugin. Are you planning a Wordpress 2.1 compatible version for the same?

2
said on 2007-02-08 at 12.34 pm

Basically the radio buttons show up on the ‘edit post’ page, but not on write new post..

3
said on 2007-02-08 at 12.49 pm

Here is the modification needed:-

Inside the function addOptions()

Replace:

strpos($_SERVER['REQUEST_URI'], 'post.php')

with:

strpos($_SERVER['REQUEST_URI'], 'post.php')||strpos($_SERVER['REQUEST_URI'], 'post-new.php')
4
Jangl
said on 2007-02-08 at 8.36 pm

Огромное спасибо за статью!

5
Jangl
said on 2007-02-08 at 11.00 pm

Только проблемки есть:

1) у меня не появилось переключателей рядом с чек боксом.

2) перестали работать комментарии, точнее ссылки на них… не находит такую страницу и саму страницу, где эта запись.

6
Jangl
said on 2007-02-08 at 11.17 pm

А всё… разобрался… я не обновил .htaccess - сейчас всё ок.

7
said on 2007-02-19 at 3.23 pm

Very prolific plugin. Concise and efficient code.
Works without any changes, even sitemaps builded
by another plugin uses new category in permalink.
Definetly a must have.

Now I’m going through all posts, assigning main cat, having fun.

8
said on 2007-02-22 at 1.16 am

Jayant, thanks for the patch. Will integrate it shortly. Plugin works with Wordpress 2.1 without any changes :-)

9
said on 2007-02-23 at 2.46 am

Great plugin! Works fine with WordPress 2.1 without changes.
Thanks for sharing!

Best wishes,
Caio Proiete

10
said on 2007-03-21 at 7.21 am

Thanks for the plugin! I’m wondering, if, in the case of such master-categories, it wouldn’t be easier to simply define certain categories that are excluded from the permalinks instead of having to define that for all posts?

Thanks!

11
said on 2007-03-21 at 8.03 am

Hi again, I’ve looked at your code and changed it by using this to select the first (in my case usually the only) category assigned to a post apart from the master-categories (here 1/16/32/33). There is a plugin that allows a main category that I am using. Having that and your category checkbox makes the admin panel unintelligible for the authors who aren’t admins. Too much stuff to do before posting a post. So I hope this simplifies it while still benefitting from your great idea and plugin -

::HLIGHT_BLOCK_1::

I’m sure this could be done far more elegantly by someone who’s more knowledgeable about php and WP, but it does the trick. Maybe someone else will benefit as well.

Thanks again!

12
said on 2007-03-21 at 8.04 am

Hmm, my code is not appearing - any formatting help to post it?

13
said on 2007-03-22 at 8.07 am

Tobias, sorry for inconvenience, I really forgot about Wordpress formatting … features. Currently I’ve adopted my code highlighting plugin to use it in comments. Check formatting tips above the textarea.

To insert code sample please use <code lang="php">$your_code = "here";</code>.

Thanks!

14
said on 2007-03-26 at 2.13 am

Hi Dmytro,

I’m one of the heavy users of your great plugin, and I’ve just found a bug on it, that occurs when someone posts a comment or a trackback on some post.

For some reason, the savePost function gets called on every new comment or trackback, and it ends up updating the post_meta table, and since there is no data to update, it updates to the first category of the post (thus loosing any child category you might have choosen to be the master).

I’ve solved it by just checking if we are on the “post” page, before attemp to save anything.

Here is the solution:

/** Store category permalink in post meta */
    function savePost($post_ID) {
        /*
            Modified by Caio Proiete on 2007-03-25.
           
            Are we inside post.php or post-new.php?
        */

        if (strpos($_SERVER['REQUEST_URI'], '/post.php') || strpos($_SERVER['REQUEST_URI'], '/post-new.php')) {
            /*
                Yes, we are... It's Safe to save/update post_meta table
            */

            $category_permalink = $_POST['category_permalink'];

            $post_category = wp_get_post_cats('', $post_ID);
            if (isset($category_permalink)){
                $found = false;
                foreach ($post_category as $cid)
                    if ($cid == $category_permalink) {
                        $found = true;
                        break;
                    }
                if (!$found)
                    $category_permalink = $post_category[0];
                   
            } else
                    $category_permalink = $post_category[0];

            if (!update_post_meta($post_ID, '_category_permalink', $category_permalink))
                add_post_meta($post_ID, '_category_permalink'$category_permalink, true);
        }         
    }

.

15
said on 2007-03-30 at 11.32 am

Thank you, guys
I have updated plugin to version 0.2.0, and you could get it
here (yeah, I have changed its location, but old URL would work too). Later I will publish post about other plugins.

16
said on 2007-08-09 at 1.52 pm

After using your plugin I’m noticing some bugs. It’s doing the opposite of what the original Word Press was doing. However, it’s actually very manageable this way! Here is how my site is setup and how your plugin is helping it.

My site has 3 main categories, let’s just say “A, B, C” and in them they have sub categories, “cat, dog, mouse”

The main categories have low # category id’s because I created them first. The way the original wordpress would work was that it simply would call on the lowest id #, as you very well know. But now with your plugin, it’s calling on the higher id # which works like this:

If category A’s ID is #10
and category dog’s ID is #22 (and dog is a sub category of A) here is how they show up.

In word press it showed up as http://www.test.com/A/title.html

With your plugin it shows up as http://www.test.com/A/dog/title.html

And that is exactly how I wanted it! This makes the categories very manageable because now I can simply add a new category and it’ll work right away.

With that said, I have not tested if it works without sub categories. I personally have a gaming site, so something like http://www.test.com/news/xbox/ is what I want. Either way, I think this plugin is a life saver

Post a comment

You can use simple HTML-formatting tags (like <a>, <ul> and others). To format your code sample use <code lang="php">$a = "hello";</code> (allowed languages are ruby, php, yaml, html, csharp, javascript). Also you could use <code>$a = "hello";</code> and its syntax would not be highlighted. If you are not using <code> tag, replace < sign with &lt;.

Submit Comment

 
Copyright © 2005 - 2008, Dmytro Shteflyuk