Replacing Self Closing Line Break Tag from Post Content

Despite the fact that more and more websites are moving towards HTML5 standard because of numerous advanced features, WordPress seems to be naive enough to stick around xHTML. Since I personally don’t like this idea, I am taking things in my own hand and follow the HTML5 standard whenever it’s possible.

Generally the_content function that retrieves your blog post seems to get <br/> (self closing line break) tag automatically from wpautop function. Since HTML5 uses <br> instead of <br/>, I had to figure out a way to resolve this issue.

add_filter('the_content','html5_line_break_tag', 20);
function html5_line_break_tag($content) {
    return str_replace('<br />','<br>', $content);

This idea is fairly simple. Filter the post content and replace all the <br/> tag with <br> and then show the content. To do this, I simply created a function and used str_replace PHP function to replace all <br/> with <br> and hooked it up with the_content function.

All you need to do now is to copy and paste the above snippet on your theme’s functions.php age and update it. That’s all.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.