WordPress – How to Escape & Strip HTML tags from the Post Title A tutorial explaining how to properly use the get_the_title() and the_title_attribute() WordPress functions to properly format the Post Entry Title

Today I received an e-mail from Richard of Comparitech (thank you Richard, by the way!)  telling me that there was an odd issue with our blog’s social sharing feature.

I made a quick check and it turned out that he was absolutely right:

Wordpress - How to Escape & Strip HTML tags from the Post Title

The outlined text clearly shows a bunch of badly stripped HTML tags. It took to me a while to understand why there were HTML tags within the Post Title: the reason was due to the Subtitles Worpdress Plugin, which we recently installed to allow our contributors to write subtitles to their posts.

The next thing I did was checking the plugin documentation, which led me to an extremely detailed and well-written FAQ page on GitHub. A quick look was enough to find exactly what I was looking for:

There’s Weird HTML Showing Up On My Site!

 

I can almost guarantee that the reason this is happening is because your theme developer is using either   or   in places where they should not be used. This is a theme bug, not a plugin bug.

 

When titles are used as attributes, the appropriate template tag to use is  , never  .

Please see these long threads as examples of what happens when themes conflict with Subtitles.

The original text can be found here, and is a good proof that the plugin author – Philip Arthur Moore from We Cobble – is a competent developer, which definitely knows what he’s talking about. The issue, just as he says, was related to our Hitmag Pro WordPress Theme, or – to better say that – to the implementation of its Social Sharing built-in feature.

Here’s what I found in the theme source code – more precisely, within the   file:

As we can see, they used the    function to retrieve the post entry title, thus causing the badly striped HTML tags issue. Replacing it with the     function was more than enough to fix the problem for good:

Unfortunately, WordPress core doesn’t provide a     wrapper function yet… Although there are some TRAC tickets asking for it (such as this one). Luckily enough, we can use the above syntax to achieve that same result: it might be a bit ugly, yet it works!

I suggested such workaround to the Hitmag Pro theme developers with a post on their theme’s official support forum: it should definitely be the proper fix for such issue, as the   WP function correctly escapes the title and also strips all the HTML tags, which is definitely a good practice for a social sharing feature (for the post title, at least).

For additional info regarding these WordPress functions, I strongly suggest to take a look at the aforementioned Subtitles plugin FAQ page and also to this relevant article from the pippingplugins.com website.

That’s it for now: I sincerely hope that this post will help other developers like me who will stumble upon this issue.

Happy posting… and social sharing!

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.