Blog

Term Meta Generator

Introducing our fresh new Term Meta Generator

Yes! It’s finally here! A new tool that will help you add various Meta-fields to your custom Taxonomies. Introducing our new Term Meta Generator.

 

Some History

WordPress 2.3 is my all-time-favorite release. It saved me tons of client complains when it introduced the “Custom Taxonomies” feature. Do you remember the times we used to create custom Categories for content that needed to stand out? like the “Featured” category, it was such a must-have category back in 2007. The new “Tags” taxonomy was added in WordPress 2.3 and a new era has come!

It took two years until WordPress core was ready for the next leap. WordPress 2.9 and 3.0 made it easy to create our own Custom Taxonomies. Since then, no major improvements from a developer point of view were seen.

The next major leap was when core replaced shared-terms across multiple taxonomies with separate-terms. The long-term Taxonomy Roadmap architecture changes were expect to happen in WordPress 3.7 or even 3.8, but the process was finally finished in WordPress 4.3. WordPress 4.4 introduces the next leap – custom meta-fields for Taxonomy Terms.

 

Term Meta Generator

Our fresh Term Meta Generator helps you to register Custom Fields for your Taxonomy Terms. It’s easy to use our tool, you don’t have to know code: just choose a Taxonomy and add Custom Fields. That’s it.

The Generator will create code that is going to:

  • Add new fields to the Create Screen.
  • Add new fields to the Edit Screen.
  • Save the data to the the database.

The class will look like this:

View snippet Clone snippet Download snippet

 

Create Screen

WordPress allows us to add custom form fields to the Taxonomy admin screens using the {$taxonomy}_add_form_fields action. We will hook to this action from the class constructor, using the create_screen_fields() method that we will add the custom fields.

Add custom fields to taxonomy terms
Add custom fields to taxonomy terms

 

Edit Screen

To add custom form fields to the taxonomy edit screens we will use the {$taxonomy}_edit_form_fields action. We will hook to this action from the Class constructor, using the edit_screen_fields() method.

Edit custom fields in taxonomy terms
Edit custom fields in taxonomy terms

 

Saving the Data

To save the fields we added, we need to hook into create_{$taxonomy} action. To save the fields we edit, we will hook to edit_{$taxonomy} action. Fortunately, to save data we can use the same callback. This is done using the save_data() method.

Our callback method needs to sanitize the user data before saving to the database. The saving process is not a rocket science, it’s done using the good old update_term_meta() function.

 

Example

We will extend the “Region” taxonomy to add three more fields: Title Color, Title HTML Tag and Region Video. The color field will show the builtin WordPress color picker, the output will be sanitized using core hex sanitizetion function. The Title Tag field will show a select box with predefined options H1-H6. And for the video field we will use the oEmbed functionality to fetch the video from YouTube or any other provider.

I should mention that the generator adds all needed markup, uses sanitizetion function and much more.

View snippet Clone snippet Download snippet

 

Enjoy!

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups, providing plugin development services.

7 Comments:

  • CDK Computers

    Great news, very useful article. Thx!

    Reply

  • WPServer.com

    Thanks for a nice article Rami!

    Reply

  • Tran Ngoc Tuan Anh

    Awesome. The code looks very nice!

    Reply

  • Ralf Wiechers

    Hey!

    Love the new generator. Got released just in time for me when I needed it.
    One more very good reason to pay for GenerateWP.

    It would be nice if the field type also could be “file from media library”. So Users could select an already uploaded file or upload a new one.
    Then getting the attachment-/post-id as a value to use.

    Greetings
    derRALF

    Reply

  • Harvey

    What is the best way to display our term meta on a custom post type page?
    Let’s say our taxonomy name is “taxonomy”, and we have 2 term_meta fields: “meta-field-1” and “meta-field-2”, how would I display the contents of those fields?

    Reply

  • Harvey

    Whats the best way to display the data from the term_meta fields on a custom post type page?
    Toda Rabbah!

    Reply

  • Rami Yushuvaev

    Hi Harvey, the best practice is to use the get_term_meta() function.

    Reply

Leave a Reply