How to add stripslashes to get_option()

683 views Asked by At

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)?

2

There are 2 answers

1
rnevius On BEST ANSWER

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.

3
Pᴇʜ On

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