$shortname = "nuod";

array( "name" => "Custom Logo URL",
    "desc" => "Enter the link to your site's custom logo.",
    "id" => $shortname."_logo",
    "type" => "text",
    "std" => "newlogo.png")

How do i make the std be automatically set as the value for <?php echo get_option('nuod_logo'); ?>


As @Ashfame as already pointed out, you shouldn't store defaults in the database - that should be for user selected options (of course, if they select the defaults, then fine - store them :).

However, you don't need to use wp_parse_args() either. get_option allows you to select a default value. For instance:

//If nuod_logo is not found, uses 'newlogo.pnp'
$number = get_option('nuod_logo', 'newlogo.png') 

However, often themes and plug-ins (as they should) keep their options in an array which is stored in one row in the database. So

$my_plugin_options = get_option('my_plugins_options') 

is expected to return all options. So you could keep all options in a default array $my_plugin_defaults:

$my_plugin_options = get_option('my_plugins_options',$my_plugin_defaults) 

but this isn't great either - you have to redeclare $my_plugin_defaults which is simply duplicating code, making room for bugs, ugly - or you make it a global variable which is just plain wrong. The solution is to create your own 'get_option' which builds on WordPress' settings API:

 function wpse28954_get_option( $option_name='' ){

      $defaults = array(
          // Array of defaults: option => default value 
      $options = get_option('my_plugins_options',$defaults);

      //Parse defaults again - see comments
      $options = wp_parse_args( $options, $defaults );

      if( !isset($options[$option_name]) )
           return false;

      return $options[$option_name];


This could be improved by allow wpse28954_get_option to set a default value in case its not in the defined defaults

Now your defaults are stored in one, easy to manage place and you can use wpse28954_get_option['my-option'] to return the saved setting or else the default value.


AS @Ashfame has pointed out in the comments using wp_parse_args has the advantage of providing defaults for an unsaved subset of options. I've updated my answer to include this. (This makes the $defaults in get_option rather redundant). +1 for @Ashfame's solution which was first to suggest using wp_parse_args.

  • IMO, wp_parse_args() is still better because in using the default parameter of get_option(), that kicks in only when get_option() doesn't return anything from database. It won't be useful in the case when you have a subset of options filled in and wants to fill the missing ones from the defaults, like when you are upgrading your plugin, and all existing users of that plugin doesn't have the newly added setting in their database yet. We would want the default to kick in for that. – Ashfame May 6 '12 at 7:03
  • Very good point! I've updated my answer to include this. :) – Stephen Harris May 7 '12 at 0:15

This is important which most of the people don't get right. Don't save the defaults in the database. You should be using wp_parse_args() for this purpose.

$defaults = array (
    'logo' => 'http://domain.com/logo.png',
    'do_extra_thing' => false

// Parse incomming $args into an array and merge it with $defaults
$options = wp_parse_args( $options, $defaults );

When $options is empty, like you have just installed a plugin, it can work off without writing anything in your database. And when it has some values inside it, the missing ones will come from $defaults array.

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.