Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created August 1, 2010 21:21
Show Gist options
  • Save mikeschinkel/503778 to your computer and use it in GitHub Desktop.
Save mikeschinkel/503778 to your computer and use it in GitHub Desktop.
<?php
/*
* Example code showing how to hook WordPress to add fields to the taxonomny term edit screen.
*
* This example is meant to show how, not to be a drop in example.
*
* This example was written in response to this question:
*
* http://lists.automattic.com/pipermail/wp-hackers/2010-August/033671.html
*
* By:
*
* Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
*
* NOTE:
*
* 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_taxonomy_term_type($taxonomy,$term_id) {
return get_option("_term_type_{$taxonomy}_{$term->term_id}");
}
function update_taxonomy_term_type($taxonomy,$term_id,$value) {
update_option("_term_type_{$taxonomy}_{$term_id}",$value);
}
//This initializes the class.
TaxonomyTermTypes::on_load();
//This should be called in your own code. This example uses two taxonomies: 'region' & 'opportunity'
TaxonomyTermTypes::register_taxonomy(array('region','opportunity'));
class TaxonomyTermTypes {
//This initializes the hooks to allow saving of the
static function on_load() {
add_action('created_term',array(__CLASS__,'term_type_update'),10,3);
add_action('edit_term',array(__CLASS__,'term_type_update'),10,3);
}
//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) {
add_action("{$tax_name}_add_form_fields",array(__CLASS__,"add_form_fields"));
add_action("{$tax_name}_edit_form_fields",array(__CLASS__,"edit_form_fields"),10,2);
}
}
// This displays the selections. Edit it to retrieve
static function add_form_fields($taxonomy) {
echo "Type " . self::get_select_html('text');
}
// This displays the selections. Edit it to retrieve your own terms however you retrieve them.
static function get_select_html($selected) {
$selected_attr = array('text'=>'','user'=>'','date'=>'','etc'=>'');
$selected_attr[$selected] = ' selected="selected"';
$html =<<<HTML
<select id="tag-type" name="tag-type">
<option value="text"{$selected_attr['text']}>Text</option>
<option value="user"{$selected_attr['user']}>User</option>
<option value="date"{$selected_attr['date']}>Date</option>
<option value="etc" {$selected_attr['etc']}>Etc.</option>
</select>
HTML;
return $html;
}
// 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">Type</label></th>
<td>$select</td>
</tr>
HTML;
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['tag-type'])) {
update_taxonomy_term_type($taxonomy,$term_id,$_POST['tag-type']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment