1

I have been searching all afternoon for a solution and couldn't find one. I am writing a plugin which needs to store regex expressions in the WP DB. An example expression is the following.

/(http:\/\/)(.*?)(example.com)/i

Storing the expression seems to work fine with update_option() and the entry in the database has the backslashes escaped with another backslash. However, when retrieving the option using get_option() the escape backslashes aren't removed, and I need to apply stripslashes() to get rid of them.

Is this a reliable method to manage the storage and retrieval of backslashes in the WP DB? Are they missing a stripslashes() or similar when the get_option() is used to retrieve the expression from the database?

0

Wordpress by default "escapes" all slashes and quotes in all input (historical reason, relic of the time PHP could have been configured to do it by default, or not). If you are handling forms by yourself instead of using the APIs you will need to strip the slashes, probably best to be done when saving the data.

  • I'm using the update_option() and get_option() functions from the Options API, so I just sort of assumed it would handle the slashes both going into the DB and retrieving out of the DB. I have applied a php stripslashes() to the result from get_options() and it seems to be doing the trick so far. Thanks for the confirmation. – Brian Jun 20 '17 at 13:55
  • APIs do not know the source of the data. probably the settings API handles it, but the others just do not have any way to know if a slash is a true slash or an escape character – Mark Kaplun Jun 20 '17 at 15:24
  • That seems to be the case. I'm just afraid my solution with the stripslashes() call on the output data will break at some point or with another type of slashed data. In any case, I've accepted your answer because it provides some independent confirmation. – Brian Jun 20 '17 at 15:31

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.