Expand Your “Edit Category” Admin Panel

If you are interested in expanding your post, page, or link administration panel, visit this step by step tutorial. Here, we describe how to add new Edit Category fields.

The easiest way to add new Category fields is by using the edit_category_form_fields action hook (line 2). This hook calls your_input_box function during rendering of the WordPress Edit Category page. Just add the hook into the functions.php file of your WordPress theme or plugin.

add_action ( 'edit_category_form_fields', 'your_input_box');

The your_input_box function is used to render additional category input fields. In the function below, we add a drop-down menu containing all the terms for our new Category theme attribute.

function your_input_box($category) {

	// Get all themes - the hide_empty argument enables you to get all the terms in your 'theme' taxonomy even the empty ones
	$themes = get_terms('theme', 'hide_empty=0'); 

?>
<tr class="form-field">
	<th scope="row" valign="top"><label for="category_theme"><?php _e('Category Theme') ?></label></th>
	<td>
    <select name='category_theme' id='category_theme' onchange="themeChange()">

	<!-- Get all themes and display them as options -->
    <?php 
		foreach ($themes as $theme) {
			echo "<option class='theme-option' value='" . $theme->name . "'>" . $theme->name . "</option>\n"; 
		}
	?>
	</select>  
	 </td>
</tr>

<?php
}

What we would like now, is to have a hook into the Edit Category save function so that we can attach our new attribute to the WordPress Category object.

The WordPress Category object is a WordPress taxonomy object. As of WordPress release 2.8.4, there is no core way to expand taxonomy objects with additional attributes.

There are plugins that try to provide this functionality, but they may not work on WordPress 2.9. In addition, if you are planning to release your theme or plugin, you may not want to tie your code to functions that are not in the WordPress core system.

Since we cannot easily add a new attribute to the Category taxonomy object, we can use the existing category_description attribute to piggyback our new data. One way to achieve this, is to attach JavaScript code to our drop-down menu in the your_input_box function. We do this by adding an onchange attribute to our select form.

    <select name='category_theme' id='category_theme' onchange="themeChange()">

Next, we add the following JavaScript before this HTML statement –

<tr class="form-field">
<script type='text/javascript'>

function themeChange() {
	// Get category description
	var desc = document.getElementById('category_description').value;
	var theme = document.getElementById('category_theme').value;
	// Piggy-back theme in category description
	var startPos = desc.indexOf("SHIBA THEME: ");
	if (startPos >= 0) {
		var stopPos = desc.indexOf("]", startPos);
		var oldTheme = desc.substring(startPos, stopPos);
		var newTheme = "SHIBA THEME: " + theme;
		desc = desc.replace(oldTheme, newTheme);

	} else {
		desc = desc + "\n[SHIBA THEME: " + theme + "]";

	}
	jQuery('#category_description').val(desc);

}

</script> 

This JavaScript code will automatically add our new category theme attribute into the existing category_description field so that we may later retrieve it for processing.

By using the WordPress edit_category_form_fields action hook, you can style new category input fields however you want, and link them to simple meta-data.

Note -

You may update the attributes of current taxonomy objects by calling wp_update_term. This function lets us reset the name, slug, and term_group of a taxonomy object, and attach that object to additional attributes (taxonomy, description, parent).

As pointed out by R’phael Spindel below, there are various action hooks that allow you to link into wp_update_term. However, based on the current implementation of wp_update_term it will be difficult to inject our own data into the database update calls.

  • edit_terms – which hooks in before name, slug, term_group are updated.
  • edited_terms – which hooks in after name, slug, term_group are updated.
  • edit_term_taxonomy – which hooks in before term_id, taxonomy, description, parent are updated.
  • edited_term_taxonomy – which hooks in after term_id, taxonomy, description, parent are updated.
  • edit_term, edit_category – which hooks in before cleaning the taxonomy cache.
  • edited_term, edited_category – which hooks in after cleaning the taxonomy cache.

Related Articles

Comments

  1. says

    The hook to use when saving a category was called ‘category_edit’ and in WP 2.3 it was changed to a variable hook called ‘edit_{$taxonomy}’ so for catching the form fields sent from the Category edit admin screen, the hook to use is ‘edit_category’. You can do away with all the messy onChange javascript events and storing values in the category_description field.

    • ShibaShake says

      Thanks for the great information. I have expanded the article, and I think it is more accurate now.

      I still cannot see a good way to inject data into the wp_update_term database calls though. If you know of a good way, please let me know so that I can further improve the article.

      Thanks!

      • says

        Well, its been a while since I revisited this topic! But, here is new information regarding this for more recent (2.9+) releases of WP.

        Along with the variable hooks:
        created_{taxonomy}
        edited_{taxonomy} (ie: edited_category or created_category )

        Taxonomy meta data is also now handled with core functions:

        add_metadata()
        update_metadata()
        get_metadata()

        however, unless its the standard ‘Category’ taxonomy, additional tables will have to be created to store the data from the new functions

        http://codex.wordpress.org/Function_Reference/add_metadata#Database_Requirements

        With these functions and hooks, WP devs now have the capability to expand the edit category admin panel with core supported functions.

  2. says

    Ahh my bad, I mean, I was looking at the add & edit category pages.

    _________
    PS. As you get around well with WP admin hacks, is there a easy way to get around this: http://stackoverflow.com/questions/1552307/pseudo-custom-taxonomies-from-categories [reasons explained over stack overflow]

    10x,
    alex

    • ShibaShake says

      What you describe is definitely do-able –

      You can go in and change the source file, but that generally isn’t a good solution because you will lose all changes whenever you update WordPress.

      A better way is to add your new category interface using the add_meta_box command, and possibly remove the old category interface with the remove_meta_box command (??never used remove_meta_box before so don’t know for sure).

      To create your new interface function, look at the source file –
      wp-admin/edit-form-advanced.php

      There should be a function in there called –
      post_categories_meta_box

      This is what wp calls to render its existing category box. Just copy that code and alter it in your own file to create the interface that you want. Then add it into the Edit post interface using add_meta_box.

    • ShibaShake says

      Hi Pax,
      The code looks right to me – so I am not sure why it is not working. I just pasted the same code into the functions.php file of my ‘default’ theme, activated it, and the drop-down menu does appear for me in the “Edit Category” screen, right above the Update Category button.

      It will only show in the full Edit Category screen though – not on the Add Category, Quick Edit, or other related screens.

      Do you get any error messages? You may want to just do an echo in the your_input_box function and then visit the “Edit Category” source page to see if the echo comes through.

  3. ShibaShake says

    In general, you want to keep these intact –

    
    	
            
    	
    
    	 
    
    

    Can you share some of your code?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>