
How To Add WordPress Category Extra Fields
- September 10, 2015
- Leave a comment
WordPress doesn’t have a category or tag meta data table in it’s database. This could be done on any category or tag or custom taxonomy. In this tutorial, we will learn how to add extra category fields in WordPress.

First of all, we need to add the extra fields to the category form using the hook {taxonomy-name}_add_form_fields and we use a simple function that will print out the extra fields. {taxonomy-name}_edit_form_fields is for edit category form fields. We use the same function to add form fields and to edit form fields. Here the {taxonomy-name} can be our category name or tag name.
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 27 28 29 30 31 32 |
<?php add_action('{taxonomy-name}_add_form_fields','pt_category_edit_form_fields'); add_action('{taxonomy-name}_edit_form_fields','pt_category_edit_form_fields'); function pt_category_edit_form_fields ($term_obj) { // Read in the order from the options db $term_id = $term_obj->term_id; $term_metas = get_option("taxonomy_{$term_id}_metas"); $custom_field_1 = isset($term_metas['custom_field_1']) ? $term_metas['custom_field_1'] : ''; $custom_field_2 = isset($term_metas['custom_field_2']) ? $term_metas['custom_field_2'] : ''; ?> <tr class="form-field"> <th valign="top" scope="row"> <label for="custom_field_1"><?php _e('Custom Field 1', ''); ?></label> </th> <td> <input type="text" id="custom_field1" name="Cat_meta[custom_field_1]" value="<?php echo $custom_field_1; ?>"/> </td> </tr> <tr class="form-field"> <th valign="top" scope="row"> <label for="custom_field_2"><?php _e('Custom Field 2', ''); ?></label> </th> <td> <input type="text" id="custom_field_2" name="Cat_meta[custom_field_2]" value="<?php echo $custom_field_2; ?>"/> </td> </tr> <?php } ?> |
As you can see, we added new fields and all of them are in an array Cat_meta[key] because it is the only way to create one row in the options table to save all of the category’s extra fields instead of a row for each field.
Now we need to save the extra fields into the database once a user submits the category edit form and we do that using “created_{taxonomy-name}” with a function that will run through each of the submitted fields and insert them to the database using the update_option function like this.
edited_{taxonomy-name} is used to save the edited category form fields. We use the same function to save the newly created category fields and edited category fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php // save extra category extra fields hook add_action('edited_{taxonomy-name}', 'pt_category_save_form_fields', 10, 2); add_action('created_{taxonomy-name}', 'pt_category_save_form_fields', 10, 2); // save extra category extra fields callback function function save_extra_category_fileds( $term_id ) { if ( isset( $_POST['Cat_meta'] ) ) { $t_id = $term_id; $cat_meta = get_option( "category_$t_id"); $cat_keys = array_keys($_POST['Cat_meta']); foreach ($cat_keys as $key){ if (isset($_POST['Cat_meta'][$key])){ $cat_meta[$key] = $_POST['Cat_meta'][$key]; } } //save the option array update_option( "category_$t_id", $cat_meta ); } } |
From the code above, you can see that all of the extra fields we’ve added are stored in the database’s options table with the name ‘category_ID‘ , where ID is the id of that specific category we just edited and that means we can call this data in our plugins or theme files easily using the get_option function. For example, if my category ID is 25 then my code will look like the following:
1 |
<?php $cat_data = get_option('category_25'); ?> |
Hi there,
Can you explain me in which files do i need to insert each part of the code?
Best regards
Hello,
You can use this code in the functions.php of the activated theme.
Thanks for reaching us out. Let us know if you need any other assistance from us.
Regards,
—
Abdullah | WP Plugin Developer
Email: support@presstigers.com
When we move to another host by creating new db and import this db, these extra data do not storage. Have you tried?