301 Moved Permanently

Image Image Image 01 Image Image Image Image Image Image Image Image Image Image

My Simple Online Portfolio.

Hi, this is Md. Asef Jamal (Ratul),echo from Bangladesh. Define myself as a Web Programmer/Developer and currently looping through Aan-nahl for 2 years.

© Copyright 2012
Asef Ratul's Portfolio

Scroll to Top

To Top


How to add custom fields with custom taxonomy in WordPress?

In wordpress, there is an option to create custom taxonomy to a custom post type. Suppose, you create a post_type name ‘service’ and then you want to add custom category/taxonomy into that post_type. So you add that. By default this custom taxonomy has the all property like worpdress default category, such as category/taxonomy name, slug, parent and description. But if you need to add more fields to that taxonomy, then how you will do that? Don’t worry. WordPress has got that functionality too.

First you have to initialize that.

add_action( ‘init’, ‘your_taxonomy_custom_fields’ );

function your_taxonomy_custom_fields() {

// Add the fields to the taxonomy, using our callback function
add_action( ‘{taxonomy_name}_add_form_fields’, ‘add_your_tax_custom_fields’ );
add_action( ‘{taxonomy_name}_edit_form’, ‘add_your_tax_custom_fields’ );

// Save the changes made on the taxonomy, using our callback function
add_action( ‘created_term’, ‘save_your_tax_custom_fields’ );
add_action( ‘edit_term’, ‘save_your_tax_custom_fields’ );

// delete the custom fields on the taxonomy delete
add_action(‘delete_term’, ‘delete_your_tax_custom_fields’);


To add the custom fields in your custom taxonomy there are two action hooks; {taxonomy_name}_add_form_fields, {taxonomy_name}_edit_form. Here {taxonomy_name} is the name of the registered taxonomy. First hook is to show the fields in the taxonomy add form and second hook is for edit form. You can do different functionality for both case(add/edit form).

To save the custom fields we need to action hooks which you can see from the above code. The first hook will be called when a new taxonomy term will be created and second hook will be in action when the newly created taxonomy term is edited. You can do different functionality for both case(add/edit form).

You can delete the custom fields when you try to delete the taxonomy term. For this you have to use the delete_term hook.

Now we will add the internal functionality of these hook action. First add/show the custom fields in the form.

// callback function to add/show a custom field to your taxonomy
function add_your_tax_custom_fields($tag) {
global $taxonomy;

// Check for existing taxonomy meta for the term you’re editing
$t_id = $tag->term_id; // Get the ID of the term you’re editing
$t_name = $tag->slug; // Get the NAME of the term you’re editing

$custom_tax_fields = get_option( “{$taxonomy}_{$t_name}_custom_field” ); // Do the check

_e( ‘<label>My custom Tax Field: </label>’ );
_e( ‘<br>’ );
_e( ‘<input type=”text” name=”your_tax_custom_field” id=”your_tax_custom_field” value=”‘ . stripslashes_deep( $custom_tax_fields ) . ‘”>’ );


Then save the fields data.

// A callback function to save our extra taxonomy field(s)
function save_your_tax_custom_fields(  ) {
global $taxonomy, $tag;
if ( isset( $_POST[‘your_tax_custom_field’] ) ) {
$t_id   = $tag->term_id;
$t_name = $tag->slug;

$custom_tax_fields = stripslashes_deep( $_POST[‘your_tax_custom_field’] );

//save the data
update_option( “{$taxonomy}_{$t_name}_custom_field”, $custom_tax_fields );

At last you can also delete the custom fields data.

function delete_your_tax_custom_fields() {
global $taxonomy, $tag;
$t_name = $tag->slug;
delete_option( “{$taxonomy}_{$t_name}_custom_field” );


That’s it. Now you can do your own taxonomy custom fields. Thank you !!


Submit a Comment