How Add New Custom Fields On Taxonomy/Category Page Using Hooks: WordPress


Hello Friends!

In admin if you want to add a new “Custom Fields” on taxonomy/category page without any changes in wordpress core files then don’t worry šŸ™‚ , you can add your new custom field using hooks.

Screenshot

Just add given code in your theme function file.

/**
 * Add extra fields to custom taxonomy edit and add form callback functions
 */
// Edit taxonomy page
function extra_edit_tax_fields($tag) {
 // Check for existing taxonomy meta for term ID.
 $t_id = $tag->term_id;
 $term_meta = get_option( "taxonomy_$t_id" ); ?>
 <tr class="form-field">
 <th scope="row" valign="top"><label for="cat_Image_url"><?php _e( 'Category Image URL' ); ?></label></th>
 <td>
 <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
 <p class="description"><?php _e( 'Enter the full URL to the image used for this category.' ); ?></p>
 </td>
 </tr>
<?php
}
add_action( 'category_edit_form_fields', 'extra_edit_tax_fields', 10, 2 );

// Add taxonomy page
function extra_add_tax_fields( $tag ) {
 // Check for existing taxonomy meta for term ID.
 $t_id = $tag->term_id;
 $term_meta = get_option( "taxonomy_$t_id" ); ?>
 
">

<?php } add_action( 'category_add_form_fields', 'extra_add_tax_fields', 10, 2 );

/*Ā  Save the custom field value into database */

function save_extra_taxonomy_fields( $term_id ) {
 if ( isset( $_POST['term_meta'] ) ) {
 $t_id = $term_id;
 $term_meta = get_option( "taxonomy_$t_id" );
 $cat_keys = array_keys( $_POST['term_meta'] );
 foreach ( $cat_keys as $key ) {
 if ( isset ( $_POST['term_meta'][$key] ) ) {
 $term_meta[$key] = $_POST['term_meta'][$key];
 }
 }
 // Save the option array.
 update_option( "taxonomy_$t_id", $term_meta );
 }
} 
add_action( 'edited_category', 'save_extra_taxonomy_fields', 10, 2 ); 
add_action( 'create_category', 'save_extra_taxonomy_fields', 10, 2 );  

//For retrieve the value in front-end
$newterm_id='put here term id';
$term_meta = get_option( "taxonomy_term_$newterm_id" ); // Do the check Ā 
echo "<b>Custom Field Value:</b> ".$term_meta['custom_catsortby'];

That’s All šŸ™‚

Enjoy Code! || Raghunath Blog

Advertisements

Sr. Software Engineer

Tagged with: , , , ,
Posted in Wordpress
4 comments on “How Add New Custom Fields On Taxonomy/Category Page Using Hooks: WordPress
  1. […] How Add New Custom Fields On Taxonomy/Category Page Using Hooks: WordPress April 14, 2014 […]

  2. Constance says:

    Amazing issue here. I am very satisfied tto look your article.
    Thank you a lot and I’m taking a look ahead tto touch you.

    Will you kindly drpp me a e-mail?

  3. Hi,
    I am desperatly looking this functionnality but I’m still unable to make it work.

    I tried to put the code in both files (my child theme or the theme functions.php file), but it does not work in any of the two cases.

    Could you help me please?
    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: