Hacking WordPress: Use the meta_query Luke

WordPress is powerful. It is hard for anyone to deny that fact. It has years of programming and thought put into it so there is no doubt that it is a solid project. Sure, criticize the code as I roll my eyes because everyone criticizes the code. Get past that point and you realize utilizing an existing platform that focuses on users might just be the right thing to do. Battle tested so to speak.

I am writing the Hacking WordPress series to help gather my thoughts on what I am learning each week in the hopes that I can assist a newcomer in the future. This first post is about using meta_query to modify WP_Query. Come on in! The water is warm… sometimes salty.

Continue reading “Hacking WordPress: Use the meta_query Luke”

Radio Buttons With Taxonomies in WordPress

Build a Custom Walker for Radio Buttons With Taxonomies

Stephen Harris did a great tutorial in 2012 with a radio button and taxonomies. I found it a bit complicated for what I was trying to accomplish for the WP Event Calendar Plugin.

I was trying to simply replace the drop down list with radio buttons in meta boxes. The user can select a radio button and be on their way. This cannot be that hard! After doing my due diligence I found an object I could extend: Walker_Category_Checklist.  Walker_Category_Checklist is used to create hierarchical taxonomies in WordPress. I can leverage that to output radio inputs instead.

1. Add a Walker Class and Functions

The walker extends core, changes the input type to radio, and value to the name of the taxonomy. See line 44. The rest is a copy of the core walker functionality.

2. Change Drop Downs Based on Our Taxonomy

Line 61 assists us with wiring the plugin. wp_event_calendar_taxonomy_args() searches for the taxonomy we want radio inputs on and applies the post_categories_meta_box callback. This is necessary so we can pass in our walker in the next step.

3. Getting the Radio Buttons Working With Taxonomies

Line 79, wp_event_calendar_checklist_args(), does a final check that we are the correct taxonomy. It applies our custom walker and we call this with our hooks for the plugin like so:

Summary

WordPress has a lot of core classes and functions that can be extended. I found this one by digging through the documentation and feeling uneasy about reinventing the wheel. That’s what led me to Stephen’s article in the first place.

This still allows us to add new terms for the metabox and just extends WordPress’s walker functionality. You can see the full pull request for the WP Event Calendar on GitHub.