3

I have some text stored in a string $text that's filtered through the_content and therefore, wptexturize().

wptexturize() is a function that replaces various characters into more prettier ones.

Is there any way that I can unwptexturize() the string? Revert it back to what it was.

What I've danced with so far

I can replace smart quotes with regular ones like this:

$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);

But that will only cover the smart quotes, there are a lot more characters.

Another alternative is to write a custom regex to replace all these characters back to their original ones.

  • Is it a possibility to run the_content without wptexturize(), extract the string you want and then parse the content again applying wptexturize()? Otherwise a regex seems the way to go, but it's not going to be very performant. – Luis Sanz Apr 29 '16 at 11:18
  • @LuisSanz Unfortunately no, the text in $text is already converted by the time I can do anything with it. – Christine Cooper Apr 29 '16 at 11:29
  • Why is it? Is it possible for you to capture the original $text with the run_wptexturize filter without interrupting wptexturize() and get your string from it? – Luis Sanz Apr 29 '16 at 11:50
  • Does the text comes from post content? If so, you could register a [notexturize] shortcode, add it to 'no_texturize_shortcodes' to prevent texturize and use a shortcode callback that just return the content as is: add_shortcode( 'notexturize', function($args, $text='') { return $text; } ); – gmazzap Apr 29 '16 at 15:11
  • @gmazzap This is an interesting approach (thanks!) but the content we're targeting has already gone through the wptexturize makeover and there is no way around it unfortunately. We're simply going to write our own unwptexturize() which shouldn't be difficult. We'll post it here once it's done! – Christine Cooper May 7 '16 at 10:04
2

Don't think so after skimming through the wptexturize() code, but what about using the run_wptexturize filter (untested):

// Turn off wptexturize
add_filter( 'run_wptexturize', '__return_false' );

// Your text handling here ...
$text = apply_filters( 'the_content', $text );

// Remove filter
remove_filter( 'run_wptexturize', '__return_false' );

i.e. just turn off the wptexturize only for your text handling?

  • Thanks for the answer, but unfortunately, the text is already filtered through the_content at a point where I can't adjust it so this would not be possible in my scenario. – Christine Cooper Apr 29 '16 at 10:39
  • Maybe others have better ideas ;-) but I guess a code example might be helpful to better understand your setup. It sounds time consuming trying to construct a unwptexturize() function with preg_replace, but I think it would need to be "bijective". I'm not even sure wptexturize() is "injective" @ChristineCooper – birgire Apr 29 '16 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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