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)?
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:And then use Sublime to replace all instances of
get_option
withmy_stripslashes_function
…no regex required, and it’s more DRY.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