My wp version is 3.2.1

In wordpress, the apostrophe/single quotation will transfer to '

And I'd better need to transfer to %27;

I tried to edit formatting.php, in line:

$string = str_replace( "'", ''', $string ); and $url = str_replace( "'", ''', $url );, but no effect. So how to do it? Or if it is possible, I think add some remove_filter function in theme's funciton.php is more better.

(Note: only transfer apostrophe/single quotation, but keep other quotation formatting.)

BTW: if I do this change, is it safe for my wordpress? this is also I care about. Thanks.

  • %27 is the URL encoding for a single quote mark, but as far I know it has no meaning in HTML. And since WordPress already strips special characters out of post slugs (in sanitize_title_with_dashes) I'm not sure what you're trying to do where this is an issue. Can you explain a bit more? Nov 6, 2011 at 17:15
  • @goldenapples, ok, in my problem, firstly, i write some links contain ' in my post and insert into database, then after read the post from database, the ' become &#039;, <a href="facebook?search=i&#039;am">i am</a>. in fact, these links do facebook api post search. and &#039; make ie and firefox return 2 diffirent kind of result.
    – yuli chika
    Nov 6, 2011 at 18:08

1 Answer 1


If I understand correctly, you're only wanting to change the behavior of wptexturize within the href attribute of links. Here's how I would go about that problem:

Don't mess with the default behavior of wptexturize, that function is used in a lot of different filters and its behavior is pretty useful. And definitely don't hack the core files for a simple case like this... its not necessary, and it'll cause you no end of headaches down the road when you want to upgrade or anything.

If you need a filter that will urlencode the href attributes of links in your content, I would use the urlencode function built in to WordPress. This also takes care of converting spaces to plus signs and any other special cases in urls. You just have to make sure its applied before the wptexturize filter comes along. I've added it at priority 9, but any priority lower than 10 would work.

add_filter( 'the_content', 'replace_apostrophes_in_links', 9 );

function replace_apostrophes_in_links( $text ) {
    return preg_replace_callback( 
            'return $matches[1] . urlencode( $matches[2] ) . $matches[3];' ),
        $text );


A bit of explanation... the regular expression will find all <a> tags in your content. The callback function will then urlencode the href attribute, and return the result. And since this filter is applied before the default formatting filters, there won't be any single quotes left in your links for WordPress to mangle.

  • thanks, first accept your anwser, this can work. but one more question, a whole page preg_replace seems terrible, it will scan all the page html dom. will it cost more CPU and more memory during the process?
    – yuli chika
    Nov 7, 2011 at 16:15
  • 1
    Well, I was only applying the filter to the post content, not the entire DOM. But if you're concerned about conserving memory and processing speed, maybe you're better off running the filter on save_post or another similar hook before the data gets into the database, that way you'd only need to process it once. Nov 7, 2011 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.