[wp-hackers] Setting default options for plugin
Viper007Bond
viper at viper007bond.com
Fri Aug 31 23:45:59 GMT 2007
Meh, I just write a checker for when I need the data (since I need to load
it anyway), something along these lines.
function loadsettings() {
$this->settings = get_option( 'my_plugin' );
if ( empty($this->settings) ) $this->settings = $this->defaultsettings;
}
Then whenever I need to load my settings, I call that function. If I don't
need any settings for this page load, the function never gets called. This
way, I know for sure that the options get loaded (I've had mixed results
with the on-load-plugin hooks).
And as for defaults, I never save them to the database (i.e. there's no
update_option() call in that function). I don't see the need, especially
since that way I can update the defaults in a future version and it'll
change the settings for all users who haven't customized their settings via
an admin form which uses update_option().
On 8/31/07, John Blackbourn <johnbillion+wp at gmail.com> wrote:
>
> Stephen,
>
> You should use the register_activation_hook function instead of
> calling the activate_xxx action directly. Unfortunately this isn't a
> well documented function.
>
> Brief explanation here:
> http://wphooks.flatearth.org/hooks/activate_plugin-filename/
>
> John.
>
> On 8/31/07, Callum Macdonald <lists.automattic.com at callum-macdonald.com>
> wrote:
> > Hey Stephen,
> >
> > I think you want to use the activate_ action. See these pages for more
> info:
> > http://codex.wordpress.org/Plugin_API/Action_Reference
> > http://codex.wordpress.org/Creating_Tables_with_Plugins
> >
> > I've just created a plugin which does exactly that which you're welcome
> > to copy. See lines 21-46 and 165 for the relevant code. The plugin is
> > available here:
> > http://www.callum-macdonald.com/code/wp-mail-smtp/
> >
> > Cheers,
> >
> >
> > Callum.
> >
> > Stephen Rider wrote:
> > > Is there an official or "best" method for setting default options for
> > > a plugin when it is first activated? Currently I use something like
> this:
> > >
> > > if (!get_option('my_plugin')){
> > > // set default options
> > > delete_option('my_plugin_options');
> > > add_option ( 'my_plugin_options',array( 'work_properly' => true ) );
> > > }
> > >
> > > This is just "raw" inside the php file (not in a function) so it runs
> > > every time the PHP file is called. Is there a better way of doing this
> > > or am I doing things right?
> > >
> > > Stephen
> > > _______________________________________________
> > > wp-hackers mailing list
> > > wp-hackers at lists.automattic.com
> > > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > >
> > >
> >
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
--
Viper007Bond | http://www.viper007bond.com/
More information about the wp-hackers
mailing list