-
-
Save sorich87/662249 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Hook WordPress to add a category field to a taxonomny term edit screen. | |
* | |
* Modified by Ulrich SOSSOU from: | |
* | |
* 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_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 ) { | |
unset($terms[$key]); | |
reset($terms); | |
update_option("_term_type_{$taxonomy}_{$category}", $terms); | |
} | |
} | |
//This initializes the class. | |
TaxonomyTermTypes::on_load(); | |
//This should be called in your own code. This example uses the posts tags | |
TaxonomyTermTypes::register_taxonomy(array('post_tag')); | |
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 "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> | |
<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['cat'])) { | |
update_term_category($taxonomy,$term_id,$_POST['cat']); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment