Home » How to properly escape and decode HTML entities in WordPress get_the_title(), get_the_content(), and get_the_excerpt() functions to deliver to social networks

How to properly escape and decode HTML entities in WordPress get_the_title(), get_the_content(), and get_the_excerpt() functions to deliver to social networks


If you’ve ever tried to create your own sharing buttons to allow your users to easily share one of your posts to their social media accounts, you may have run into issues delivering dynamic content from get_the_title(), get_the_content(), get_the_excerpt(), etc. In this post I’ll explain how I escape and decode HTML entities in WordPress in order to prepare a string to post to social media.

Often times the issue is that you’ll often get people that create posts using content they’ve copied and pasted from MS Word; if you didn’t know, MS Word, Outlook, and most MS products create their own versions of characters like double quotes, ellipses, long dash, etc., which makes the life of a developer just a little bit harder for no apparent reason other than it’s Microsoft…

Other times you’ll just get some HTML entities that you can’t decode with the standard htmlspecialchars_decode() or html_entity_decode() functions that PHP offers. So if you see any of these HTML entities in your javascript string, or any entities at all that don’t decode as you expect, then this solution might work for you:

Here’s an example of what some HTML entities look like in case you don’t know:


Javascript Strings

Safely deliver decoded HTML entities in a javascript string so you can pass off bits of dynamic content from your post correctly:

Obviously this will work for any of the “get_” functions that have HTML entities that you’re having issues decoding. Also, the FB.setDescription is just an arbitrary example of a javascript function/method that you’d pass the string into for posting to social media; you’ll want to replace that with whatever your javascript method is. This could also just be a string in a javascript object or something similar as well.

There’s also a handy WordPress function called wp_specialchars_decode() that is, in theory, supposed to do the same thing.


SMS or WhatsApp links

This particular combination took me a bit to figure out because iPhone treats + as a literal plus sign instead of the space that it represents when using this in an anchor link:


If you continue to have issues decoding HTML entities, feel free to post a comment and I’ll see if I can help you troubleshoot. A detailed description of your encoding/decoding troubles in the comments below will allow me to try to reproduce and fix the problem with you 🙂

3 comments on “How to properly escape and decode HTML entities in WordPress get_the_title(), get_the_content(), and get_the_excerpt() functions to deliver to social networks

  1. marinading on

    I am no longer sure where you are getting your information, but
    great topic. I needs to spend some time learning more or figuring out
    more. Thank you for great information I was looking for this info for my mission.

    Reply
  2. Felix on

    Good post. I learn something new and challenging on sites I
    stumbleupon on a daily basis. It’s always interesting to
    read content from other authors and practice something from other websites.

    Reply
  3. kaworlds.com on

    It’s going to be finish of mine day, however before finish I am reading this fantastic article to increase my
    knowledge.

    Reply

Leave a Reply

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