1

Hy, I created a WordPress Theme using a lot of custom options. While saving the options in the backend amongst other things I'm adding backslashes before characters that need to be escaped. Such as ' and ".

Now I need to remove them before displaying them on the frontend. The easiest way would be stripslashes(get_option($key)). But that would mean I'd have to go though the whole Theme and change all get_option() manually.

Is there a way to add a filter to the get_option()?

If not, is there a way to achieve this with find/replace (I'm using Sublime Text 3 which allows regex)?

Nico Martin
  • 1,268
  • 1
  • 14
  • 23

2 Answers2

3

Why not just create your own function in place of get_option() (but which takes advantage of it)? For example, you could define the following in functions.php:

function my_stripslashes_function($option, $default = false) {
    return stripslashes( get_option($option, $default) );
}

And then use Sublime to replace all instances of get_option with my_stripslashes_function...no regex required, and it's more DRY.

rnevius
  • 26,578
  • 10
  • 58
  • 86
0

Yes in Sublime Text you can realize it with an regex:

find: get_option\((.[^)]*)\)
replace with: stripslashes(get_option($1))

If you need to debug a regex yourself here is a helpful tool: https://regex101.com/r/rY7oG6/2

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Thank you so much for your answer! But now it selects quite a lot stuff I don't want to be selected. For example: `if(get_option($id,'')==$o){$selected='selected';}` selects `get_option($id,'')==$o)` (But I just want `get_option($id,'')`) – Nico Martin Jun 16 '15 at 06:31
  • Thanks! But now I have one more problem to deal with: `if(get_option('the_key'.qtranxf_getLanguage(),'')=='hello'){` – Nico Martin Jun 16 '15 at 06:56
  • hah I guess that won't work. The regex catches everything between `(` and the first `)`. I don't know any reqex to find the correct `)`. I guess you can`t solve that with a regex sorry. – Pᴇʜ Jun 16 '15 at 07:12