How can I add custom meta fields in categories?
Solution 1
The problem:
Wordpress does not have a structure nor method to store "meta" values for taxonomies.
UPDATE 2017: WP 4.4+ has "term meta"!
For working with term metas use these:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()
The Actions below are still valid though! :)
Additional reading: 4.4 Taxonomy Roundup
Solution for WP version <= 4.3.x and COMMON actions
Actions:
-
create_category
andedit_category
for category edit -
category_add_form_fields
andcategory_edit_form
for category form fields
There are more actions than I've presented, but they seem to be deprecated (according to developer.wordpress.org).
The reason I chose the actions that I chose:
- They work on WordPress 4.4.2
- Due to lack of documentation I assumed these are the new ones replacing the deprecated ones...
Functions:
update_option
has two great abilities:
a) It craetes the option when such option does not exist yet
Unless you need to specify the optional arguments of add_option(), update_option() is a useful catch-all for both adding and updating options.
b) $new_value
can be an integer, string, array, or object.
You may ask, why to use array/object? ...well, because each option = 1 database row => you probably want to store your category options in one row :)
The CODE
function my_category_form_fields($tag_object){
//output/display extra form fields, e.g. by echo ...
//ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
//(see note at 'edit_category' action...)
if( !empty($tag_object['term_id']) ){
//edit category form specific
//...load existing options with get_option( $option, $default );
} else {
//create category form specific
}
}
function my_category_save(){
//CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
//(see note at 'edit_category' action...)
//SECURITY CHECK
if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
return null;
//save your form values using update_option()
//Recommendation:
//Add "category_" prefix and $category_id to your option name!
}
add_action( 'create_category', 'my_category_save', 10, 1 );
//Runs when a category is updated/edited,
//INCLUDING when a post or blogroll link is added/deleted or its categories are updated
//(which causes the count for the category to update)
add_action( 'edit_category', 'my_category_save', 10, 1 );
add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 );
add_action( 'category_edit_form', 'my_category_form_fields', 10, 1 );
Create or Edit?
You might wonder whether you are creating or saving a category - this not documented yet (as far as I know), but from testing:
- Edit save =>
$tag_object
isobject
and contains some properties, most notably:term_id
taxonomy
filter
- Create save =>
$tag_object
is just a regularstring
"category" - I guess this might change in the future...
General taxonomy
There are also actions like these for taxonomies in general - check these actions.
Solution 2
Jaz, It looks like the plugin you mention in your original question has been updated to include a checkbox field (included in v1.2.3)
Solution 3
I think the Category SEO Meta Tags plugin will help you.
Solution 4
There is an updated and refactured version of this plugin to be found here:
https://wordpress.org/plugins/custom-taxonomy-category-and-term-fields/
Also added a WYSIWYG editor fieldtype.
Related videos on Youtube
cooljaz124
Im a designer and Front end Developer, passionate of learning the new tricks of my trade and specialize in web standards. Wordpress Lover.
Updated on June 04, 2022Comments
-
cooljaz124 almost 2 years
Does anyone have any idea how to add custom meta fields while making categories and fetch them in the loop in WordPress? I was wondering how to do that without hacking the WordPress core, but if I do – it won't become a hindrance to update WordPress in the future.
A plugin I have found that comes close is Wp-Category-Meta, but it doesn't have the ability to add checkboxes as fields in Edit Categories.
This will be very useful as users can make certain categories "featured", and then the code can use that meta value in the loop to style "featured" categories differently.
-
jave.web about 6 yearsOf course there are also plugins that can do all this for you :) Important is to look whether they can use the WordPress' own *_termmeta table in 4.4+ , it is then reusable and does not create unnecessary tables :)