Unlock your potential with a Pro Membership
Here is where you propel your career forward. Come join us today. Learn more.
Add HTML to Content and Return it Back
Lab: PHP Output Control and Buffer Basics
Video Runtime: 13:38
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 this:
add_filter( 'the_content', __NAMESPACE__ . '\add_markup_to_content' ); | |
function add_markup_to_content( $content ) { | |
$html = '<div class="entry-content-container" itemprop="text"><div class="wrap">'; | |
$html .= $content; | |
$html .= '</div></div>'; | |
return $html; | |
} |
You’ll use the PHP output buffer and move the HTML into a view file to separate it from the business logic. Let’s do this together, step-by-step.
Other Resources Mentioned in the Episode
- Lorem Ipsum – dummy content to create posts in your sandbox site.
- Variable Scoping
- Variables when including files episode – to understand how the variable is usable within the view file.
Who needs copy/paste? Right. Writing code from scratch is fun!
Episodes
Total Lab Runtime: 00:44:16
- 1 Lab Introductionfree 07:54
- 2 Setting Up the Labfree 08:24
- 3 Add HTML to Content and Return it Backpro 13:38
- 4 Shortcode View Filepro 10:04
- 5 Wrap it Uppro 04:16