• Resolved Guido

    (@guido07111975)


    Hi,

    I’m using get_option() quite a lot inside my plugin, to retrieve settings from my DB.

    Many settings are checkboxes, with value “true” or “false”.

    By default I use escaping:

    
    esc_attr( get_option('my-setting') );
    

    My question: is it necessary to escape the output in this case? The value in DB is always “true” or “false”. And I do sanitize upon input.

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • Are you actually printing out “true” or “false”? You would only need to use esc_attr if you are outputting a HTML attribute. That function escapes quotes so that the attribute comes out right.
    Look at the example on this page: https://developer.www.ads-software.com/reference/functions/checked/

    Also, please use an array for your options, instead of a separate option for each one. (like in the example) Using separate options makes a mess of the options table.

    Thread Starter Guido

    (@guido07111975)

    Hi Joy,

    Thanks for your reply.

    Yes, something like this:

    
    if (get_option('my-setting') == "true") {
      do stuff
    }
    

    I did not know about an array for options, so I guess you mean storing multiple values in a single option instead of an individual option for each value. Will look into this for sure, because I have about 50 values which I now store in 50 options..

    Guido

    The checked() function is for output of the checkbox, but your code example is not doing output, so not sure what we are talking about. You should not trust database values, and always escape when output, but comparing to a known value is a fine.

    But definitely use an array.

    Thread Starter Guido

    (@guido07111975)

    Hi Joy,

    The checked() function was not really related to my question.
    I’m using that if condition to do stuff.. or not. If my checkbox is checked it outputs a “true” value, otherwise “false”. And I’m only using checkboxes for if conditions throughout my plugin:

    
    if (get_option('my-setting') == "true") {
      do stuff
    } else {
      do nothing
    }
    

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using the get_option() function and escaping’ is closed to new replies.