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.
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 editcategory_add_form_fields
andcategory_edit_form
for category form fieldsThere 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:
get_option( $option, $default );
update_option( $option, $new_value, $autoload );
update_option
has two great abilities:a) It craetes the option when such option does not exist yet
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
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:
$tag_object
isobject
and contains some properties, most notably:term_id
taxonomy
filter
$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.
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)
I think the Category SEO Meta Tags plugin will help you.
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.