Collapsible Categories in Admin for WordPress

UPDATE: If you want to more easily manage categories in the admin, you don’t need my code anymore. Someone created a plugin which makes this whole thing much easier. Here it is:
http://wordpress.org/extend/plugins/intuitive-category-checklist/

Here’s my original post for posterity :))   :

If you have many, many categories in WordPress, it can be a pain to manage them all.  I wrote some code to do this using jQuery,  and thought I would share.  First you need to override WordPress’ default behavior for categories, which is to show the selected categories at the top, removing them from their position in the tree.  You can bypass this by installing the Category Checklist Tree plugin.  Without this plugin installed, my code will not help you much.

What my code does is to first collapse the whole categories tree, so that all subcategories are hidden, but it only does this if none of the subcategories are selected.  If a subcategory is selected, then that portion of the tree is shown in full.  My code adds “expand” and “collapse” links to the left side of each category that has subcategories.

Instructions
1. Install  Category Checklist Tree plugin.

2. Download this code, save it to the root of your template folder and name it admin_category_stuff_js.js

2a. IF you want to hide subcategories even when the parent category is selected, then change line 10 to this:

if ($j(this).children(‘.children’).find(‘input:checked’).length == 0) {// if no subcats selected

3. In the functions.php file, add this at the end (this code will load the script whenever you are in the admin:

add_action(‘admin_init’, ‘admin_category_stuff’);

function admin_category_stuff() {
$template_url = get_bloginfo(‘template_url’);
wp_register_script(‘admin_category_stuff_js’,$template_url.’/admin_category_stuff_js.js’);
wp_enqueue_script(‘admin_category_stuff_js’);
}

That should do it! I welcome feedback on the code and will try to help you if you have trouble with it. I’ve tested in 3.0.x – 3.3.1.

14 comments

  1. Works perfect! Thank you so much.

  2. hi, i tried but i it doesn’t work maybe i didn’t understand will.
    first i activate the plugin then put the script file in my theme/mytemplate/
    and copy the script in function.php. but nothing happens…?

    • Chad Phillips

      It seems you have understood the steps. Which version of WordPress are you using? Nothing happens at all?

  3. works perfect in 3.2.1

  4. Owizardo

    Hi there, im on 3.4 (Beta) is it still working at that version? Let me know I would love to get this working since I got so many categories and makes it messy the way it’s left.

    • Chad Phillips

      I’ve tested in 3.3.1 and it works great. I think you’ll have good luck in 3.4 – please let me know!

  5. Thank you man! It’s working!

  6. can you make it work also for wp e-commerce product categories?

    • Chad Phillips

      To make the wp-ecommerce product categories collapsible in the admin, you’ll want to make this adjustment:
      1. Open admin_category_stuff_js.js
      2. Find this: categorychecklist (you’ll find it in three places)
      3. Replace it with this: wpsc_product_categorychecklist
      4. Upload the file and that should do it!

      Note: if you make this change, the normal categories will no longer collapse. If you want the normal admin categories and the wp-ecommerce product categories to both be collapsible, you have to do this:
      1.Open admin_category_stuff_js.js
      2. Copy everything from line 8 ( $j(‘#wpsc_product_categorychecklist li’).each( function () { //foreach li )
      through line 35 ( }); )
      3. Paste everything below line 35 but before the final closing bracket/parenthesis/semicolon.
      4. Now make the changes above but only to the lines after 35, so you’ll make three replacements just as before.
      5. Upload the file and both types of categories will be collapsible!

      Hope that helps…

  7. Amazing! That was really helpful, thank you!

  8. thanks for this code :) works fine with 3.8 – http://wordpress.org/extend/plugins/intuitive-category-checklist/ seems to be broken since 3.4.
    so 10000000x thanks for this. i was looking for weeks for something like this :)

Leave a Reply

Current month ye@r day *