Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Admin / Custom Taxonomy / Expand Your “Edit Category” Admin Panel

Expand Your “Edit Category” Admin Panel

by ShibaShake 10 Comments

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.

Expand Your Edit Category Admin Panel

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
}

Screen-shot of the Edit Category admin panel. The newly added input field is shown at the bottom.

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. R'phael Spindel says

    February 6, 2010 at 3:49 pm

    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.

    Reply
    • ShibaShake says

      February 6, 2010 at 8:51 pm

      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!

      Reply
      • R'phael Spindel says

        June 22, 2011 at 5:49 pm

        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.

      • ShibaShake says

        June 22, 2011 at 9:21 pm

        Thanks R’phael .

        These functions are indeed very useful and I have written about them in Add Term and Taxonomy MetaData.

  2. pax says

    October 18, 2009 at 2:14 pm

    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

    Reply
    • ShibaShake says

      October 18, 2009 at 7:33 pm

      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.

      Reply
  3. pax says

    October 18, 2009 at 2:06 pm

    here: http://img269.imageshack.us/img269/6249/shibaprintscreen2.jpg
    so I just pasted the first 2 code snippets in your article, at the beginning of functions.php within the Default Theme.

    The I have checked the ‘Add New Post’ page where I couldn’t see any new inputs.
    Am I doing things as supposed to? 🙂

    Reply
    • ShibaShake says

      October 18, 2009 at 7:04 pm

      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.

      Reply
  4. ShibaShake says

    October 11, 2009 at 7:17 pm

    In general, you want to keep these intact –

    
    	
            
    	
    
    	 
    
    

    Can you share some of your code?

    Reply
  5. pax says

    October 11, 2009 at 5:33 pm

    I have followed your steps on a fresh install of WP 2.8.4
    but nothing happened in the new/edit post page.

    Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·