class wpturbo_CustomTermMeta{
private $fields = array(
);
public function __construct(){
if ( is_admin() ) {
// Register all the hooks.
add_action( 'category_add_form_fields', array( $this, 'wpturbo_render_meta_fields' ), 10, 2 );
add_action( 'category_edit_form_fields', array( $this, 'wpturbo_edit_meta_fields' ), 10, 2 );
add_action( 'created_category', array( $this, 'wpturbo_save_meta_fields' ), 10, 1 );
add_action( 'edited_category', array( $this, 'wpturbo_save_meta_fields' ), 10, 1 );
}
}
// Render fields on the add taxonomy page.
public function wpturbo_render_meta_fields($taxonomy){
$html = '';
foreach( $this->fields as $field_id => $field ){
$meta_value = '';
if ( isset( $field['default'] ) ) {
$meta_value = $field['default'];
}
$field_html = $this->wpturbo_render_input_field($field_id, $field, $meta_value);
$label = "<label for='$field_id'>{$field['label']}</label>";
$html .= $this->wpturbo_format_field($label, $field_html);
}
echo $html;
}
// Render fields on the edit taxonomy page.
public function wpturbo_edit_meta_fields($term, $taxonomy) {
$html = '';
foreach( $this->fields as $field_id => $field ){
$meta_value = get_term_meta( $term->term_id, $field_id, true );
$field_html = $this->wpturbo_render_input_field($field_id, $field, $meta_value);
$label = "<label for='$field_id'>{$field['label']}</label>";
$html .= $this->wpturbo_format_field($label, $field_html);
}
echo $html;
}
// Format every field to table display.
public function wpturbo_format_field($label, $field){
return '<tr class="form-field"><th>'.$label.'</th><td>'.$field.'</td></tr>';
}
// Render each individual field.
public function wpturbo_render_input_field($field_id, $field, $field_value){
switch($field['type']){
case 'select': {
$field_html = '<select name="'.$field_id.'" id="'.$field_id.'">';
foreach($field['options'] as $key => $value){
$key = !is_numeric( $key ) ? $key : $value;
$selected = '';
if($field_value === $key){
$selected = 'selected="selected"';
}
$field_html .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
}
$field_html .= '</select>';
break;
}
case 'textarea': {
$field_html = '<textarea name="'.$field_id.'" id="'.$field_id.'" rows="6">'.$field_value.'</textarea>';
break;
}
default: {
$field_html = "<input type='{$field['type']}' id='$field_id' name='$field_id' value='$field_value' />";
break;
}
}
return $field_html;
}
// Called when the taxonomy is saved.
// Save the new meta values for our taxonomy.
public function wpturbo_save_meta_fields($term_id){
foreach ( $this->fields as $field_id => $field ) {
if( isset($_POST[$field_id]) ){
// Sanitize fields that need to be sanitized.
switch($field['type']){
case 'email': {
$_POST[$field_id] = sanitize_email($_POST[$field_id]);
break;
}
case 'text': {
$_POST[$field_id] = sanitize_text_field($_POST[$field_id]);
break;
}
}
update_term_meta( $term_id, $field_id, $_POST[$field_id] );
}
}
}
}
if (class_exists('wpturbo_CustomTermMeta')) {
new wpturbo_CustomTermMeta();
}
Term Meta Generator
Create category, tag or custom taxonomy meta fields quickly and easily with this WordPress term meta generator. Choose the taxonomy and add all the needed fields. Then, they will appear in the WordPress admin. This way, you’ll get a better control over their WordPress data, as well as improve the user experience of managing taxonomies in WordPress.