arrow-left arrow-right brightness-2 chevron-left chevron-right facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Related Entries apostrophe error
1 min read

Related Entries apostrophe error

As many of you have noticed and routinely bring to my attention, the Related Entries WordPress plugin (you can see it in use on the sidebar when looking at an individual archive) breaks any time the title of the post for which related entries are being sought, contains an apostrophe. I e-mailed the author a few months ago, but he couldn’t nail down the problem.

I came up with a pretty elaborate solution that I’ve been using for the past week or so, but have since abandoned. Basically, I split the title of the post, delimited by spaces, into its constituent words. I then cycled through the array of words and used preg_replace() to remove any apostrophes and letters that followed them. Finally, I cycled through the now-modified array of words and concatenated them with each other to form the title sans apostrophes.

After slapping myself for wasting so much time I came to my senses and remembered the addslashes() function; a single call to this function fixes the problem. Feel free to grab my hacked version of the plugin and do with it what you will. Be aware that my code has the find_keywords() function removed (I don’t use it and I doubt many of you do either).

You don’t need to worry about the possible double escaping of anything because the term being escaped (i.e., the title) is never inserted back into the database. It might also be prudent of me to mention that addslashes() is not needed if your web server’s PHP installation has magic_quotes_runtime enabled, but of course if that is the case you probably aren’t seeing the apostrophe error in the first place.

You've successfully subscribed to Justin Blanton.
Success! Your account is fully activated, you now have access to all content.