Today I am going to explain about to create extra field into category section or custom Taxonomy in WordPress.
Sometimes in WordPress, we need some extra fields into category or custom taxonomy like an image for a category or detailed description etc. So we thanks to WordPress that provides action hooks using that we can create action hooks in that without any plugin.
Yes, without a plugin, It’s better to code itself because plugin might have some extra code or code which can’t support in next version of WordPress. So try to minimize the use of plugins in WordPress and code by yourself. It’s quite easy to write such a code in WordPress.It may take few mins.
Let’s check out code and understand:
First of all, let’s create action hook for adding html
for new field into category or Custom Taxonomy.
Read about :To Configure Auto Updates in WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | add_action('{$taxonomy}_edit_form_fields','edit_form_fields'); add_action('{$taxonomy}_edit_form', 'edit_form'); add_action('{$taxonomy}_add_form_fields','edit_form_fields'); add_action('{$taxonomy}_add_form','edit_form'); // your desired code } function edit_form_fields ($tag) { $termid = $tag->term_id; $cat_meta = get_option( "tax_$termid"); ?> <th valign="top" scope="row"> </th> <td> <textarea name="description_detail" id="description_detail" rows="5" cols="40" class="large-text"><?php echo $cat_meta;?></textarea><br /> <span class="description">Entering detail description</span> </td> </tr> <?php } |
Next,action hooks
are for saving that new field into a database after created or updated.
Also Read: Customize Search in WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // when the form gets submitted, and the new field gets updated (in your case the option will get updated with the values of your custom fields above add_action ( 'edited_{$taxonomy}', 'save_extra_fileds'); add_action('created_{$taxonomy}','save_extra_fileds'); // save extra category extra fields callback function function save_extra_fileds( $term_id ) { if ( isset( $_POST['description_detail'] ) ) { $termid = $term_id; $cat_meta = get_option( "tax_$termid"); if ($cat_meta !== false ) { update_option( "tax_$termid",$_POST['description_detail'] ); }else{ add_option( "tax_$termid",$_POST['description_detail'] , '', 'yes' ); } } } |
well, If you create some fields in the term, then you need to remove that field when delete term
. so next filter hook is about for same.
1 2 3 4 5 6 7 8 9 10 11 | // when a category is removed function remove_tax_Extras($term_id) { $termid = $term_id; if($_POST['taxonomy'] == '{$taxonomy}'): if(get_option( "tax_$termid")) delete_option( "tax_$termid"); endif; } |
Below action hooks are to display newly created field into display section of taxonomy which is dependent on a version of WordPress too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | add_filter( 'manage_edit-{$taxonomy}_columns', 'taxonomy_columns_type'); add_filter( 'manage_{$taxonomy}_custom_column', 'taxonomy_columns_type_manage', 10, 3); function taxonomy_columns_type($columns) { $columns['keywords'] = __( 'Detailed Description', 'dd_tax' ); return $columns; } function taxonomy_columns_type_manage( $out ,$column_name, $term) { global $wp_version; $out = get_option( "tax_$termid"); if(((float)$wp_version)<3.1) return $out; else echo $out; } |
That’s it. Like I said, quick but exceptionally handy.All you need to do is just copy and paste above code into your theme’s functions.php file and replace {$taxonomy}
with your desired taxonomy.
As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (6)