Fix  character in Google search results and Facebook share description

If you’re seeing a strange  character or characters in your Google search results description or when sharing your articles and blog posts on Facebook but not in your article body itself then trust me, I understand your frustration because I thought I had tried every solution out there and none of the common fixes seemed to work for me. Read more to find out how I fixed the problem for my website and hopefully it will help you as well.

When it’s not in the body

If and only if you’re not seeing those characters anywhere else except for when you share your post on Facebook or do a simple Google search for your article then the solution will most likely be tied to how you’re telling Facebook and Google what your website is about.

Yep, I’m talking about the Facebook’s Open Graph properties and Structured Data that Google looks at. Have you been trying to implement these features into your website recently by any chance?

Double check your code

After spending hours of trying to figure out what the problem was and after creating this video to demonstrate the issue I finally realized the problem wasn’t related to WordPress at all. The problem was me and more importantly my lack of understanding the difference between two of the most widely used PHP functions: htmlentities(); and htmlspecialchars();

For a long time I was using the following code to give Facebook my article’s description:

<meta property="og:description" content="<?php { setup_postdata($post); echo esc_attr(htmlentities(get_the_excerpt())); } ?>"/>

And there’s nothing wrong with that… other than the fact that if you happen to have a special character somewhere in your content, it’s going to be translated literally.

Check out the htmlentities(); description

all characters which have HTML character entity equivalents are translated into these entities

If I had read the description beforehand none of this would have happened!

The trick was to replace the function with htmlspecialchars(); which doesn’t translate any special characters and the output remains clean.

<meta property="og:description" content="<?php { setup_postdata($post); echo esc_attr(htmlspecialchars(get_the_excerpt())); } ?>"/>

Once you’ve made the change make sure to re-scrape your content using Facebook’s debugger.

If the character is in the body

If you’re seeing the character all over your article’s body and you have no idea what I just talked about above then your solution could be as simple as putting the following code somewhere in your head tags:

<meta charset="UTF-8">

Was this helpful? Let me know in the comments below!

Author: ynef

29 year old freelance writer and web developer. A technophile who appreciates and respects mother nature in all of its glory. Favorite skills in Runescape include Farming, Herblore and Magic.


You may also like