Brad Williams Blog

WordPress and Stuff

  • Home
  • About Brad
  • Contact Brad
  • Professional WordPress Plugin Development
  • Professional WordPress
You are here: Home / Archives for metadata

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

July 1, 2010 By Brad 11 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 the Founder of WebDevStudios.com, a co-host on the SitePoint Podcast, and the author of Professional WordPress and Professional WordPress Plugin Development.

Brad resides in Philadelphia.

 Subscribe in a reader

WordPress Tips and Tricks Newsletter

Sign Up

Professional WordPress Plugin Development


Order my new book!
Professional WordPress Plugin Development More Details

Order Professional WordPress

Professional WordPress Book Cover
Professional WordPress
More Details

Upcoming Events I’m Attending

WordCamp Philadelphia 2010 WordCamp Phoenix 2011
WordCamp Philly - Oct 30th
WordCamp San Francisco - Aug 12th-14th

Brad on Twitter

  • Late night munchies! (@ Steak 'n Shake w/ @aprilheline) http://t.co/vuQpGpM5 about 2 hours ago
  • I just unlocked the “Flame Broiled” badge on @foursquare! Cheeseburgers all around! http://t.co/jdIou1OF about 2 hours ago
  • If you're in downtown Indy and you aren't drunk....you're doing it wrong! #fb about 3 hours ago
  • Getting ready to watch LMFAO! #superbowl cc @dremeda http://t.co/L5yDZ83d about 3 hours ago
  • On the bus to downtown Indy. Traffic is pretty bad #superbowl 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: Run Apache HTTP Server and IIS on Windows Server 2003
  • How To: Update your Twitter status with ASP
  • How To: Download Podcasts with iTunes and Sync with a Zune
  • 10 Reasons why I’m buying a 2nd Generation Zune over any Apple iPod
  • How To: Create ASP and AJAX username availability check example
  • Xbox 360 Red Ring of Death
  • Halo 3: My Addiction!
  • How To: Keep iTunes video window "always on top"

Topics

  • Rants (13)
  • Web Development (6)
  • WebDevStudios (13)
  • WordCamp (12)
  • WordPress (57)
  • WordPress Plugins (8)

Archives

  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • 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 © 2012 · Powered by WordPress, Genesis, and WDS