I ran into a frustrating problem today with WordPress formatting an apostrophe in an unexpexted way. I am sharing this post with those of you who may run into this problem. Hopefully this will save you a couple of hours of troubleshooting.
WordPress Formats Apostrophe into Right Single Quote
WordPress converts the apostrophe literal ( ' ) into a different HTML entity code, the Right Single Quotation Mark ( ’ ). It should convert to the apostrophe entity which is ( ' ). In WordPress's documentation (wp-includes/formatting.php) you can see the annotation that this is in fact true.
Solving the problem in my situation
You can jump to the simple solution in bold below or read the steps for solving my particular problem here.
I wrote a PHP script that parses an XML data feed generated from a FileMaker database, then this data is inserted into custom tables in our WordPress database. On the front end we need to search on the 'organization' field in one of these custom tables. Everything worked well when I would test the function directly in the PHP script. But the interface setup was not returning results for organizations with an apostrophe or ampersand. Here is why.
We generate WP posts with a title that matches the name of each organization. So, the string in both the post_title field and our custom organization field are exact. The problem is when I grab the title, via the get_the_title($post->ID) function, WordPress formats the apostrophe literal ( ' ) into a right single quotation mark entity code ( ’ ). So when I pass the formatted string into a search query on our custom table's organization field, no results return. The exact or fuzzy search don't match.
Solution
The PHP function html_entity_decode() works for properly transliterated characters. For example ampersand entity code ( & ) to ampersand literal ( & ). However, the apostrophe literal ( ' ) is changed into an entirely different literal, the right single quote entity (’). It should be (') as stated earlier. So I used the following 'replace' code to solve the problem.
Note: Remove the space between the & and the #8217;
You can view the complete HTML and XML character entity reference here:
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Captain Stubing
September 12, 2012This is so stupid and annoying on the part of Wordpress. Go to any WP blog and try using Ctrl-F in your browser to find something on the page that presumably has an apostrophe in it, and watch it fail. Oh, no problem, just go ahead and type a right single quote... 999 out of 1000 users have no idea how to do that.
David
May 30, 2013Thanks Nick. This worked for me... it just seems like there should be an easier way. I guess this is just the way it is.
josh g
August 9, 2013for some reason this still isn't working for me. I'm grabbing the post title and it has an apostrophe. I'm using the post title name to SELECT from my DB. I guess it's keeping the right apostrophe instead of the apostrophe literal as it's not return data, but if I type the post title itself in the SELECT with a backslash it works.
code:
html_entity_decode(str_replace("’","'",$title));
$title is the post title. any ideas?
josh g
August 9, 2013Ok, so it took the html code and converted it haha, but I'm using your code above and took out the space
josh g
August 9, 2013I ended up doing this.
adding this line to functions.php:
remove_filter ('the_title', 'wptexturize');
and using mysql_real_escape_string in the post.
reference: http://stackoverflow.com/questions/5967234/wordpress-stripping-out-apostrophes-in-the-title
Adriana
March 26, 2014Thanks! This has been an issue for years.
Bilal Shah
March 28, 2014Thanks !
There are approx. 2000 products which have this issue and i can solve with one line.
Thanks.
Tom Pavey
October 10, 2014After hours and hours troubleshooting I stumbled upon your site to find the correct solution. Thanks man.
For me the answer was:
remove_filter ('the_title', 'wptexturize');
I've been stuck for hours on this, it seems like an incredibly short-sighted thing on wp's part.
Thanks to josh g in particular.
Ben
October 8, 2021This issue has been killing me for hours and after much trial & error and googling, this fixed it. Thanks Nick!