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.
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
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?
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.
Hi,
I have re-tested it and not found any issues, all is working fine.
Can you send me the site details on my email raghunath.0087@gmail.com where you are trying to add given code.
Thanks