Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Hook WordPress to add a category field to a taxonomny term edit screen.
* Modified by Ulrich SOSSOU from:
* By:
* Mike Schinkel (
* This could easily become a plugin if it were fleshed out.
* A class with static methods was used to minimize the variables & functions added to the global namespace.
* wp_options was uses with one option be tax/term instead of via a serialize array because it aids in retrival
* if there get to be a large number of tax/terms types. A taxonomy/term meta would be the prefered but WordPress
* does not have one.
* This example is licensed GPLv2.
// These are helper functions you can use elsewhere to access this info
function get_term_category($taxonomy, $term_id) {
return get_option("_term_type_{$taxonomy}_{$term_id}");
function update_term_category($taxonomy, $term_id, $category) {
delete_category_term($taxonomy, $term_id);
update_option("_term_type_{$taxonomy}_{$term_id}", $category);
update_category_terms($taxonomy, $category, $term_id);
function get_category_terms($taxonomy, $category) {
return get_option("_term_type_{$taxonomy}_{$category}");
function update_category_terms($taxonomy, $category, $term_id) {
$terms = get_category_terms($taxonomy, $category);
$key = array_search($term_id, $terms);
if( false === $key ) {
$terms[] = $term_id;
update_option("_term_type_{$taxonomy}_{$category}", $terms);
function delete_category_term($taxonomy, $term_id) {
$category = get_term_category($taxonomy, $term_id);
$terms = get_category_terms($taxonomy, $category);
$key = array_search($term_id, $terms);
if( false !=== $key ) {
update_option("_term_type_{$taxonomy}_{$category}", $terms);
//This initializes the class.
//This should be called in your own code. This example uses the posts tags
class TaxonomyTermTypes {
//This initializes the hooks to allow saving of the
static function on_load() {
//This initializes the hooks to allow adding the dropdown to the form fields
static function register_taxonomy($taxonomy) {
if (!is_array($taxonomy))
$taxonomy = array($taxonomy);
foreach($taxonomy as $tax_name) {
// This displays the selections. Edit it to retrieve
static function add_form_fields($taxonomy) {
echo "Category " . self::get_select_html();
// This displays the selections. Edit it to retrieve your own terms however you retrieve them.
static function get_select_html($selected = 0) {
return wp_dropdown_categories('selected=' . $selected . '&hierarchical=1&echo=0&child_of=' . get_cat_ID('T2') );
// This a table row with the drop down for an edit screen
static function edit_form_fields($term, $taxonomy) {
$selected = get_option("_term_type_{$taxonomy}_{$term->term_id}");
$select = self::get_select_html($selected);
$html =<<<HTML
<tr class="form-field form-required">
<th scope="row" valign="top"><label for="tag-type">Category</label></th>
echo $html;
// These hooks are called after adding and editing to save $_POST['tag-term']
static function term_type_update($term_id, $tt_id, $taxonomy) {
if (isset($_POST['cat'])) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.