-
-
Save sorich87/717889 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 | |
/* | |
* Example code showing how to hook WordPress to add several fields to the taxonomny term edit screen. | |
* To-Do: Add field types other than text fields. | |
* | |
* Original author: | |
* | |
* 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. | |
* | |
*/ | |
class TaxonomyTermTypes { | |
var $taxonomies = array(); | |
var $fields = array(); | |
function __construct( $taxonomies, $fields ) { | |
$this->TaxonomyTermTypes( $taxonomies, $fields ); | |
} | |
//This initializes the hooks to allow saving of the | |
function TaxonomyTermTypes( $taxonomies, $fields ) { | |
$this->taxonomies = $taxonomies; | |
$this->fields = $fields; | |
add_action( 'created_term', array( &$this, 'term_type_update' ), 10, 3 ); | |
add_action( 'edit_term', array( &$this, 'term_type_update' ), 10, 3 ); | |
$this->register_taxonomy( $taxonomies ); | |
} | |
//This initializes the hooks to allow adding the dropdown to the form fields | |
function register_taxonomy( $taxonomy ) { | |
if ( !is_array( $taxonomy ) ) | |
$taxonomy = array( $taxonomy ); | |
foreach( $taxonomy as $tax_name ) { | |
add_action( "{$tax_name}_add_form_fields", array( &$this, "add_form_fields" ) ); | |
add_action( "{$tax_name}_edit_form_fields", array( &$this, "edit_form_fields" ), 10, 2 ); | |
} | |
} | |
// This displays the selections. Edit it to retrieve | |
function add_form_fields( $taxonomy ) { | |
foreach( $this->fields as $field ) { | |
$select = self::get_text_html( $field, $value = '' ); | |
$html =<<<HTML | |
<div class="form-field"> | |
<label for="tag-$field[id]">$field[label]</label> | |
$select | |
</div> | |
HTML; | |
echo $html; | |
} | |
} | |
// This displays the selections. Edit it to retrieve your own terms however you retrieve them. | |
function get_text_html( $field, $value = '' ) { | |
$html =<<<HTML | |
<input name="tag-$field[id]" id="tag-$field[id]" type="text" value="$value" size="40" /> | |
HTML; | |
return $html; | |
} | |
// This a table row with the drop down for an edit screen | |
function edit_form_fields($term, $taxonomy) { | |
foreach( $this->fields as $field ) { | |
$field_id = $field['id']; | |
$value = get_option("_term_{$field_id}_{$taxonomy}_{$term->term_id}"); | |
$select = self::get_text_html( $field, $value ); | |
$html =<<<HTML | |
<tr class="form-field form-required"> | |
<th scope="row" valign="top"><label for="tag-$field[id]">$field[label]</label></th> | |
<td>$select</td> | |
</tr> | |
HTML; | |
echo $html; | |
} | |
} | |
// These hooks are called after adding and editing to save $_POST['tag-term'] | |
function term_type_update($term_id, $tt_id, $taxonomy) { | |
foreach( $this->fields as $field ) { | |
$field_id = $field['id']; | |
if ( isset( $_POST['tag-' . $field_id] ) ) { | |
update_option( "_term_{$field_id}_{$taxonomy}_{$term_id}", $_POST['tag-' . $field_id] ); | |
} | |
} | |
} | |
} | |
//This initializes the class. | |
//This should be called in your own code. | |
$tax_fields = array( | |
array( | |
'id' => 'more', | |
'type' => 'text', | |
'label' => 'More Link' | |
), | |
array( | |
'id' => 'keywords', | |
'type' => 'text', | |
'label' => 'Keywords Link' | |
), | |
array( | |
'id' => 'programs', | |
'type' => 'text', | |
'label' => 'Affiliate Programs Link' | |
), | |
); | |
new TaxonomyTermTypes( array( 'category' ), $tax_fields ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment