WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on creating a plugin to help me with future development projects. I want to make sure what I am using best practices when I write it. My question is about the use of get_option in my plugin.

I have setup an options page for the plugin and have set a couple options there. Is it better to check the option on the add_action step or with the function that is being added. I have written up a short example to help illustrate my question. Thanks!

if ( get_option('my_option[option_1]') ==='1' ) {
  add_action('init', 'my_action')


add_action('init', 'my_action');
function my_action() {
  if ( get_option('my_option[option_1]') ==='1' ) {
    //do stuff here

My thoughts are that if I check get_option first then I can set it as a var in my plugin and then just check the var anytime I need it. But as I said I am new to plugin development so I don't fully understand the best way to use get_option.


share|improve this question
up vote 4 down vote accepted

Both ways are almost equal, the first will be slightly faster, because the callback is called only if the check equals to TRUE.

Note you cannot test an option like this, unless the option name is really 'my_option[option_1]'. What you probably want is:

if ( $test = get_option('my_option') and 1 === $test['option_1'] )

An extended example:

$my_options = get_option('my_option');

if ( 1 === $my_options['test_1'] )
    add_action( 'init', 'my_init' );

if ( 1 === $my_options['test_2'] )
    add_action( 'shutdown', 'my_shutdown' );

// clean up
unset( $my_options );
share|improve this answer
Great, thanks for the help! So based on your update could I set $test to the options array at the top of my plugin and check against it for all the options as I need them? – yoxalld Jan 9 '13 at 16:37
@yoxalld See my update. – toscho Jan 9 '13 at 16:44
That's great! Thank you so much for the help! – yoxalld Jan 9 '13 at 16:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.