$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: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
is expected to return all options. So you could keep all options in a default array
$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:This could be improved by allow
wpse28954_get_option
to set a default value in case its not in the defined defaultsNow 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.Edit
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
inget_option
rather redundant). +1 for @Ashfame‘s solution which was first to suggest usingwp_parse_args
.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.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.