How to fix apostrophes turning into slashes in WordPress
We build our sites with a custom theme options panel in the Dashboard so that clients can manage as much of their site on their own as possible. All seemed fine and dandy until the client threw in an apostrophe! The chutzpah. Can’t you just use “We will” instead of “We’ll”?
The problem was that when an apostrophe was entered into a custom text label in the options panel, a slash decided to come along for the ride. Example: Instead of seeing “We’ll send you a link”, the site displayed “We\’ll send you a link”. I searched around and finally found this post, Using stripslashes() with Theme Options which explains that you need to use the stripslashes function when you return the content stored in the custom text.
Basically, you just wrap stripslashes() around the get_option like so:
If you had <?php echo get_option(‘cgn_
it would now be:
<?php echo stripslashes(get_option(‘cgn_
In your custom-text.php file, you’ll probably use something like this:
Category: Code Snippets










I have a suspicion you are doing something wrong here. Stripslashes should be handled by WordPress internally, therefore I’m thinking you may not be using the WP API to do whatever it is that you are trying to do.
To confirm I wasn’t going mad I tried the following code, which gave the same output both times.
http://pastebin.com/Nf6aeV2H
Ryan is right, I wrote that a long time ago and it’s not what I would do anymore. Use the WordPress built in stuff (like wp_kses_post) to handle issues like the post talks about.
Yup, although you wouldn’t use wp_kses() on output as it is way too resource intensive. I made that mistake once and managed to create a plugin which was about 100x less efficient than it was supposed to be
Sorry, but you’re doing it wrong. You should never have to use functions like stipslashes (or addslashes for that matter) when working with the WordPress Options and Settings APIs. WordPress will do all of that for you, and all you need to care about is good escaping and options sanitization. Meaning use functions like esc_attr() and use a validation function to validate and/or sanitize all the user input.
Thank you so much everyone for your feedback! It’s really valuable, and we’re working to make sure we implement the WordPress APIs properly.
Hi everyone!
Actually, I just found the answer: no coding necessary, no command, no functions…
Simply add ' instead of the apostrophe then it will remove the backslash!!!
Example: Art by Aurelien’s beautiful photography available on luxurious canvas.
Will be, when typed in WordPress: Art by Aurelien's beautiful photography available on luxurious canvas.
Voila!
@Aurelien, what is so funny is that I had the OPPOSITE problem. I was using one straight quote (an apostrophe) that would spit out a backslash. In a popular free non-profit template called Foundation by Single Templates. Anyhow, I tried all of this complicated stuff to fix the issue. I finally substituted a curly apostrophe (single quote) in the place of the straight one, and as you say, voila! It’s interesting that you had a problem with the curly ones and for me the problem was the straight ones!
Thanks to the original blog poster for even writing and posting this. It was comforting to find others struggling with the same issue. This is the THEME OPTIONS part of the template, and my client absolutely has valid reasons for using plural names in the titles and blurb text that appears on the homepage.
@June, you’re amazing!!! Not only my previous post here has been modified (chances are that this site has stripped the little code I had suggested) but my solution worked if you only had one apostrophe per sentence…
BUT now, I’ve substituted all of them by curly ones and voila!!!
Finally, an easy solution…
Thank you so much June, you rock!