There are times when you need to control the PHP output buffer. Shortcodes are a good example of when you want to capture the buffer and return it to WordPress instead of sending it out to the browser. In this hands-on lab, you will dive into the functionality available to you for the PHP output control.
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.
Setting Up the Lab
Let’s walk through how to setup your sandbox to work with me through the lab episode. You will need: PHP Output Buffer Companion plugin Kint For Kint, you can use either my Kint PHP Debugger plugin, which is in the WordPress repository, or UpDevTools.
Wrap it Up
Let’s finish up with a quick review. Plus, I’ll show you how I do it in my code here on Know the Code. And I’m giving you a challenge too.
Shortcode View File
Using the same strategy, let’s apply it to a shortcode. In this episode, you’ll do the following: Move the HTML out of the shortcode function and put it into a View file Start the output buffer Load the view file Then get the HTML out of the buffer, clean the buffer, and return it back to WordPress You are leveraging the PHP output buffer to separate out the HTML into a view file. Why? We’ve covered that many times. However, for you, it’s so much easier to deal with native HTML instead of placing it into strings and storing it […]
Add HTML to Content and Return it Back
There are a lot of use cases where we want to add HTML, continue processing, and then render it out to the browser. WordPress has multiple filter events that let us tap into the process just before it goes out. It gives us the ability to change it. One example is when you call the function the_content(). There is a filter with the same name. In this episode, you’ll use the companion plugin you installed in the last episode. Then you’ll add more HTML to the content. Next, I’ll show you the way I see so often which is like […]
Lab Introduction
Let me introduce you to what you will be doing in this lab and why it matters. PHP gives you the ability to capture what you are sending out to the browser. Instead of sending it immediately out, you capture it in a buffer. Why would you want to do that? In order to continue processing the HTML. Some good examples are: Shortcodes Post title filter Content filter For each of the above, you need to return the HTML back to WordPress, the theme, or the plugin that needs to continue processing it. A design pattern that I often see […]
Test, Fine-Tune, and Wrap it Up
Let’s test our plugin to make sure everything works as expected. Open up your browser and go to the Console tab. Look for errors. If you find an asset not found error, you’ll need to explore why. I found when testing that Yoast SEO has an admin stylesheet that has the version number as part of the filename. That causes a problem. Therefore, I added the enqueued handle to my configured list of assets to skip. If you want the final code for this project, click here to view it on GitHub .
Improving Our Code & Readability
In the last episode, we parsed the URL and then used the host element to check if the URL is local to our website. Shouldn’t we use that same strategy for our version query string check? Think about it. Why search the entire URL for the version key pattern when we could just search the query itself? Plus, if the asset was enqueued with the version number set to null, then query may not even be in the parsed URL. Let’s improve our code. Then we’ll walk through all of the checks and make each one readable without an inline […]
Only the Local Assets
If a stylesheet or script is not local to the website, then we do not want to convert or apply a version number to it. Right? The file for that asset does not reside on our website’s web server. Nope, it’s somewhere else. Examples of this are: Font Awesome Google Fonts Bootstrap Zurb’s Foundation As part of our checks, we want to verify if the asset’s URL is local to our web server. How do we do that? Think about what would make a URL local.
Skip When No Version Query String
What if no version query string was applied to the asset’s URL? Then there’s no reason for us to run the conversion code. Right? That regex pattern is slow. So we don’t want to run it unless it’s needed. When does WordPress not append a version query string? Think about it. Go look up wp_enqueue_script. Look at the description for the version parameter. In this episode, we’ll explore 2 different techniques of checking if the incoming asset’s URL has the version query string.