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.
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: