Brad Williams Blog

The Life of an Internet Junkie

  • Home
  • About Brad
  • Contact Brad
  • WordPress Plugins
    • Custom Post Type UI
    • CollabPress
    • WP Manage Plugins
    • Post Google Map
    • WordPress Theme Showcase Plugin
    • WP-Devel
    • WordPress MU Category Mapping Plugin

How To: Save Taxonomy Meta Data as an Options Array in WordPress

July 1, 2010 By Brad 7 Comments
  • Tweet

One challenge in WordPress is saving meta data on a taxonomy. For instance imagine you want to save additional data on a category or tag. Currently there is no wp_termmeta database table to store taxonomy meta data. In this tutorial I’m going to show you how to save a single ID against every category in WordPress. I’m going to save that meta data (the ID in this case) as a single array of options in the WordPress options table.

First we need to add a form field to the Edit Category screen. To do this we’ll use the edit_category_form_fields action hook available in WordPress:

<?php
add_action ( 'edit_category_form_fields', 'tme_cat_featured');

function tme_cat_featured( $tag ) {

	//check for existing featured ID
	$cat_featured = get_option( 'category_featured' );
	$featured_id = '';
	if ( is_array( $cat_featured ) && array_key_exists( $tag->term_id, $cat_featured ) ) {
		$featured_id = $cat_featured[$tag->term_id] ;
	}
?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="category_featured"><?php _e('Featured Post ID') ?></label></th>
        <td>
        	<input type="text" name="category_featured" id="category_featured" size="3" style="width:5%;" value="<?php echo $featured_id; ?>"><br />
            <span class="description">The post ID that will be the featured post when viewing this category.</span>
        </td>
    </tr>

<?php
}
?>

The above code will add a single text field (Featured Post ID) to the Edit Category screen as shown in the screenshot below:

Now that we’ve added a text field to our Edit Category page we need to save the input to our option array. To capture the ID when a category is updated we’ll use the edited_category action hook like so:

<?php
add_action ( 'edited_category', 'tme_save_featured');

function tme_save_featured( $term_id ) {
	if ( isset( $_POST['category_featured'] ) ) {

		//load existing category featured option
		$current_featured = get_option( 'category_featured' );

		//set featured post ID to proper category ID in options array
		$current_featured[$term_id] = intval( $_POST['category_featured'] );

		//save the option array
		update_option( 'category_featured', $current_featured );
	}
}
?>

As you can see we are saving the Category ID and Featured Post ID as a single associative array option value in WordPress named category_featured. As you add Featured Post ID values to each of your categories they will be added to this array and saved in WordPress. One thing to consider when using this method is scalability. If you plan on storing thousands of meta data values for your taxonomies this isn’t the method for you.

I packaged this example into a stand alone plugin so you can easily install on your WordPress website and test it out: Download the Taxonomy Meta Data plugin example.

Filed Under: How To, PHP, WordPress Tagged With: metadata, taxonomy, tutorial, WordPress

Who is Brad?

Brad Williams picture

Brad Williams is a computer programmer and tech junkie who enjoys exploring technology and sharing his knowledge and experience with others.

Brad is currently the CEO at WebDevStudios.com, a co-host on the SitePoint Podcast, and the author of Professional WordPress.

Brad resides in Philadelphia.

 Subscribe in a reader

WordPress Tips and Tricks Newsletter

Sign Up

Order Professional WordPress

Professional WordPress Book Cover
Order my new book!
Professional WordPress

Upcoming Events I’m Attending

WordCamp Philadelphia 2010 BlogWorld Badge
WordCamp Mid-Atlantic - Sept 11th
Blog World Expo - Oct 14-16
WordCamp Philly - Oct 30th

Brad on Twitter

  • Secret obsession (watching Cheers via @gomiso) http://miso.io/9WxJzu about 6 hours ago
  • I've figured out the secret of blogging...write random thoughts, blurbs, etc at night and cleanup/publish in the morning! #genius about 7 hours ago
  • @BizGirl @strebel + @williamsba @amber331 + Vegas = Trouble #bwe10 about 7 hours ago
  • Holy frack! Awesome news RT @photomatt: A New Home for the WordPress Trademark: http://wp.me/p4oB3-9ie #wordpress about 7 hours ago
  • I owned "Brad Williams" on Google at one point in my life, about 2.5 years ago: http://w.illiams.com/b4kPNm about 8 hours ago
  • Follow Brad

Most Popular Posts

  • How To: Return an Xbox 360 for repairs
  • Free Motorola Q USB Device Drivers
  • How To: Update your Twitter status with ASP
  • 10 Reasons why I’m buying a 2nd Generation Zune over any Apple iPod
  • How To: Run Apache HTTP Server and IIS on Windows Server 2003
  • How To: Download Podcasts with iTunes and Sync with a Zune
  • Xbox 360 Red Ring of Death
  • How To: Create ASP and AJAX username availability check example
  • Halo 3: My Addiction!
  • How to: Update FriendFeed using ASP

Topics

  • ASP/.NET
  • Blog Updates
  • How To
  • Personal
  • Philadelphia
  • PHP
  • Podcasts
  • Professional WordPress
  • Projects
  • Rants
  • Speaking
  • Web 2.0
  • Web Development
  • WebDevStudios
  • WordCamp
  • WordPress
  • WordPress Plugins

Archives

  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006

Sites I Like

  • Pluginize
  • Two and the Zoo
  • WebDevStudios
  • WordCamp Philly
  • WordPress Tavern
  • WP Classroom

Return to top of page

Copyright © 2010 · Powered by WordPress, Genesis, and WDS