elegant themes

How to fix apostrophes turning into slashes in WordPress

| April 15, 2012 | 9 Comments

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_404_text’); ?>

it would now be:

<?php  echo stripslashes(get_option(‘cgn_404_text’)); ?>

In your custom-text.php file, you’ll probably use something like this:

<?php echo  stripslashes($selected_book_description); ?>
 
No more slashtastrophies!
 

Tags: , , , ,

Category: Code Snippets

About Rebecca Markowitz: Rebecca Markowitz has built over 50 WordPress sites for clients as Web Project Manager and WordPress specialist at illuminea. illuminea is a Jerusalem-based boutique web agency. WPGarage shows my dedicated relationship with WordPress over the years - full of love, laughs, tears, growth and strong drinks. L'chaim! View author profile.


Comments (9)

Trackback URL | Comments RSS Feed

  1. Ryan Hellyer says:

    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.

  2. Ryan Hellyer says:

    To confirm I wasn’t going mad I tried the following code, which gave the same output both times.

    http://pastebin.com/Nf6aeV2H 

    • curtismchale says:

      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.

      • Ryan Hellyer says:

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

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

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

  5. Aurelien says:

    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!

  6. June says:

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

    • Aurelien says:

      @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!

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Code Snippets (4 of 34 articles)