[wp-hackers] Need help with: coding new Plugin options
Otto
otto at ottodestruct.com
Thu Nov 6 21:11:42 GMT 2008
Yeah, I'm forced to agree there. I just tested that generator, and
this is what it gave me:
function getAdminOptions() {
$adminOptions = array("optionName" => "Value",
"optionName2" => "Value",
"optionName3" => "Value");
$savedOptions = get_option($this->adminOptionsName);
if (!empty($savedOptions)) {
foreach ($savedOptions as $key => $option) {
$adminOptions[$key] = $option;
}
}
update_option($this->adminOptionsName, $adminOptions);
return $adminOptions;
}
That's just poor practice, man. Here's a better way:
function getAdminOptions() {
return get_option($this->adminOptionsName, array(
// default option values follow
"optionName" => "Value",
"optionName2" => "Value",
"optionName3" => "Value"
));
}
Simple, no? Look, this function is just supposed to get the options,
correct? So why is it writing anything to the database at all, ever?
The default values don't need to be in the database until they are
altered or saved. Then it can update them. The rest of the time, as
long as the plugin loads its options using getAdminOptions there, then
it will always get either the options from the DB or the defaults. Who
cares where they come from?
-Otto
On Thu, Nov 6, 2008 at 2:54 PM, Ozh <ozh at planetozh.com> wrote:
> On Thu, Nov 6, 2008 at 9:49 PM, Daiv Mowbray <daiv at daivmowbray.com> wrote:
>> Ozh, the functions which you refer are from the plugin builder:
>> http://www.wp-fun.co.uk/wizzards/fun-with-plugins/
>> It reads to see if the options are there, if not they write the default into
>> the ops table.
>> If you know another way to check for options ... all ears.
>
> A lot of plugins do this on every instantiation:
> 1) read options from DB
> 2) do various checks, including default values
> 3) write options in DB
>
> The write part is totally unneeded and is just a query waste, since
> after step 2 everything is ok. Just to steps 1 and 2, and write to the
> DB only when options are updated through the admin form.
>
> My 2 cents :)
>
>
> --
> http://planetOzh.com ~ Blog and WordPress Stuff
> http://FrenchFragFactory.net ~ Daily Quake News
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list