Search code examples
phparrayswordpressif-statementkey

Use get_option in an IF statement in WordPress


I'm trying to activate a functions written in php whenver a checkbox is checked. The checkbox leaves an empty option in de database, when enable it shows

my-general-options with value: 
a:1:{s:13:"my-custom-option-1";s:1:"1";}

Therefore the options page is working correctly right?

The code I'm using is:

      if ( get_option('my-custom-option-1') === '1')  {   
        echo 'hello world</div>';
      }

Also tried:

      if ( get_option('my-general-options', my-custom-option-1') === '1')  {      
        echo 'hello world</div>';
      }

What I'm I missing?


Solution

  • With WordPress get_option() function, use the option_name (not the option_value).

    enter image description here

    So if your option_name is for example "this-is-my-option-key", use it as follows:

    $option_value = get_option('this-is-my-option-key');
    
    if ( isset( $option_value['my-custom-option-1']) && $option_value['my-custom-option-1'] == 1 ) {
        echo '<div>' . __('hello world') . '</div>';
    }
    

    It should work.