In this episode, you and I are going to refactor the FAQ module. We’ll have both shortcode versions and the archive template using the same container and FAQ view files. You’ll hear and see me walking through the thought and build process, thinking about different approaches, and then testing it out. It’s a good exercise of reducing the codebase and thinking about the build process. Come do this with me.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Planning the Refactor
As you were building this module, did you notice the redundancies within the view files? We are using separate view files that have virtually the same HTML structures to them. That’s not good. Why? Because if you need to make a change to the structure or one of the styling attributes, you have to remember to change it in three different places. That is an error waiting to happen. If you forget about one of them, then the change might get released and pushed out to your customers. That’s problematic. Instead, the better strategy is to combine the shortcode and […]
Finish by Topic Shortcode
In this episode, you’ll finish building the loop code to render out each FAQ. We’ll use the same FAQ view file. Therefore, we need to keep the variables named the same. Since we are in the loop and we setup the post within WordPress Core, i.e. when we did $query->the_post(), we can use the API wrapper functions, such as get_the_title() and get_the_content(). Then we need to order the FAQs by the menu order. We’ll set both the order and orderby parameters. To finish up, let’s document our code now by adding the DocBlocks. We’ll also type hint the function parameters […]
Continue Building the Shortcode
In this episode, we’ll continue building the shortcode for the topic. We’ll look move the while loop to a new function to process the individual FAQs. As we are building, notice that we have a decision to make: Do we display a message when no FAQs are found? Let’s do that. But instead of echoing at each message, let’s make it configurable and give access through the shortcode’s user-defined attributes. Then the author (or you) can turn it off and on as well as change the messages. This is a refactoring exercise for you. We start off by making sure […]
Add a FAQ Feature to the Collapsible Content Plugin – Part 2
In this hands-on lab, you and I will walk through the entire process of adding a new feature to an existing WordPress plugin. You’ll start with the Collapsible Content plugin and then add a FAQ feature to it. This feature will require you to plan, think, and execute building a custom post type, custom taxonomy, advanced SQL queries, custom archive, and shortcode.
Let Theme Override Our Archive Template
In the last episode, you wrote the code to serve the archive template for the FAQ custom post type from the plugin. Now we need to allow the theme to override. If a developer wants to change the archive, s/he could add an archive-faq.php file to their theme. In that case, you’d want to load their template instead of yours. Give the power to the theme over the plugin for templates. There are a couple of approaches to accomplishing this feature. You’ll first build a solution with Technique 1 that locates the template, and if not available, it then returns […]
PHP 101: Building Strings
We need to build a string from dynamic content. We’ll have a string literal and we need to insert some content into it at the appropriate spot. How do we do it? There are a couple of techniques you can use in PHP including embedding variables and functions and using a formatted string with sprintf or printf.
Test Entry Footer Terms
Next, we need to test our new rendering code. Does it work on each of the pages and only for those that have terms? Let’s test it together. Here is the department taxonomy term rendered into the entry footer of a post: Here it is for a team bio single: Then we need to commit our work to GitHub to save it. Let’s do that together.
PHP 101: Concatenating Assignment Operator
The term “concatenating” means that we are smooshing two strings together by appending the one on the right to the one on the left. The result is a new string value. For example, let’s say that a variable $post_meta has a value of ‘[post_categories] [post_tags]‘. You want to append another shortcode to the end of it. How do you do that? There are two different ways to achieve this and make an assignment.
Render Entry Footer Terms
In this episode, you and I are going to explore the Genesis framework. You’ll learn about the [post_terms] shortcode available to you in Genesis and how to customize its user-defined attributes to add the taxonomy and text before the labels. You’ll learn about the “genesis_post_meta” filter event too, as you will register a callback and then take a deep dive into its processing.