Setting Object Terms With A Checkbox in WordPress

I’m going to start this article out with a thought that I feel to be true. At times, in our professional lives, we tend to become lazy. In most cases, we don’t do so consciously. This seems to happen because we know where to find answers and we end up relying upon those sources. Then, when we are not able to find the answer through a previously used source, we don’t always know what to do. Prior to immediate access to an endless supply of information via the internet, we had to answer our own questions and solve our own problems. So was the case with a recent client project.

Here’s the scenario:

  • The client needed to be able to tag a particular article as a “Cover Story”.
  • The solution was not to use a WordPress “Tag” or “Category”.
  • The functionality needed to be simple and so that editors could easily identify such articles.

With the requirements clearly identified, I started looking on the internet for a way, which quickly proved to be fruitless and a waste of time. I put on my thinking cap and was able to create a solution that was clear, concise, and easy to use. So, let’s talk about this solution.

Tagging An Article As A Cover Story

Not using WordPress’ built-in solution for tagging and categorizing can be simple to achieve in these type situations by utilizing a custom taxonomy. We could easily set a term in place and query that term. Creating a custom taxonomy is as easy as usingregister_taxonomy(). Let’s see an example of a registered taxonomy:

The above code is standard for creating a custom taxonomy in WordPress, but with a few differences. You will notice thatshow_ui ,show_in_nav_menus , andshow_admin_column are all false. The reason for this is that the taxonomy was to in an essence hidden from the editor. This code above gave us a custom taxonomy that we could query but not be seen in the Dashboard.

Wait a minute, if the editor cannot see the taxonomy, how will the editor be able to select an article as a “Cover Story”? That is question that I had to answer.

Hook To the Publish Metabox

A high priority was to make the end result feel like it was a natural part of the WordPress UI while being clearly marked and visible. To do so, we hooked into the WordPress “Publish” metabox. Hooking into the metabox is as easy asadd_action( 'post_submitbox_misc_actions', 'function_name' ); . While hooking into the metabox proved to be easy, the ability to achieve the overall goal of being able to easily tag an article did not. Our thought was to add a checkbox to the “Publish” metabox. Adding a checkbox is easy, but having said checkbox set a term and remove a term, was not easy. Let’s look into the solution.

Adding the Checkbox

Let’s first look at how we were able to add a checkbox.

As you can see from the example above, we have created a standard checkbox that has a value of “cover-story” and is in return usingchecked() for whether the post has that term.  We are usingis_object_in_term() to determine if the current post has the “cover-story” term. Lastly, we are hooking into the “Publish” metabox as described above.

Saving the Term

Now that we have created the checkbox and hooked it to the “Publish” metabox, we need to save the data that is being passed. In effect, when the user checks the checkbox, the cover-story term needs to be applied to the current post. Also, if the user un-checks the checkbox, the term needed to be removed from the current post.

This is our solution:

A few items of note from the above code are:

  • If the user has checked the checkbox for the cover story, the “cover-story” term would need to be associated with the current post. We usedwp_set_object_terms() to achieve this. Note that the final option in this usage is true. This function has an boolean option to$append to the taxonomy. By default, the value is false. Since we would like for additional terms to be used for this taxonomy in the future, the value of$append needed to be set to true.
  • If the user later un-checked the checkbox, the “cover-story” term would need to be removed from the current post. We usedwp_remove_object_terms() to do this.

UI Love

As stated above, we wanted the solution to feel natural and match the UI of WordPress. To do so, we applied some css to provide the proper experience. This css was included in the parent theme’s admin stylesheet.

The above css is using Sass as a css pre-processor.

Here is a screenshot of the result:

The visual end result
The visual end result

Final Result

We verified that the terms were being applied or removed from the correct post (the correctterm_taxonomy_id was being applied to the correctobject_id inthe _term_relationships table in the database), while ensuring that the checkbox fit into the overall UI of WordPress. We wrapped this in a class and called it a day!

As always, I’d love to know your thoughts on this solution!