You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhancement: for options page - ability to autoload=false for options #1093
When creating an options page using \new_cmb2_box() give ability to define the autoloader, so devs could define that those options should NOT be autoloaded.
As a developer, you know the drawbacks of autoload=yes.
Actual Behavior:
currently it defaults to yes
Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):
// If no override, update the optionreturnupdate_option( $this->key, $this->options );
Either a filter there (the easiest path), that will allow to add a 3rd param to update_option() call, or a param that is passed to new_cmb2_box() call and should be passed down to the set() method.
Also, you are right. It will not switch the autoload value in the DB after the first time it is inserted. If you want to test switching, you will need to actually delete the option from the database first.
Just remember, that update_option() doesn't fire $wpdb->update() to save data if old/new values are the same.
So the bug in the commit is that you default to null, which in case of already saved no uses null as default that later omits the $update_args['autoload'] (which is redefined only if not $autoload !== null) completely and don't pass the updated value to ->update() (that's why current code can't swtich from no to yes).
In short, you need to always pass the boolean no matter what, instead of a null.
Looks good, tests for both filter and param for all scenarios passed fine when true/false is used.
There will be a problem with 'no' string, but I'm not sure something should be done here, at your discretion. Core is explicitly check 'no' and false, but we (as developers) want strict types :)
Activity
slaFFik commentedon Feb 18, 2018
In
CMB2_Options::set()we have this:Either a filter there (the easiest path), that will allow to add a 3rd param to
update_option()call, or a param that is passed tonew_cmb2_box()call and should be passed down to theset()method.Ability to autoload=false for options. Closes #1093
jtsternberg commentedon Feb 19, 2018
Added a filter, and parameter on options-page boxes toggles the filter. Please test: https://github.com/CMB2/CMB2/compare/options-autoload-feature
slaFFik commentedon Feb 22, 2018
@jtsternberg I've tested the PR.
Switching from
yestonovia both filter and param work.Swithcing back from
notoyesdoesn't work, it always stays the sameno.jtsternberg commentedon Feb 22, 2018
@slaFFik can you provide your code that you tested with?
jtsternberg commentedon Feb 22, 2018
Also, you are right. It will not switch the autoload value in the DB after the first time it is inserted. If you want to test switching, you will need to actually delete the option from the database first.
slaFFik commentedon Feb 22, 2018
Default code from https://github.com/CMB2/CMB2-Snippet-Library/blob/master/options-and-settings-pages/options-pages-with-tabs-and-submenus.php snippet (100% the same) +
or
slaFFik commentedon Feb 22, 2018
Using this
actually udpates autoload
notoyes. So you don't need to remove option and update it again to change this value.slaFFik commentedon Feb 22, 2018
Just remember, that
update_option()doesn't fire$wpdb->update()to save data if old/new values are the same.So the bug in the commit is that you default to
null, which in case of already savednousesnullas default that later omits the$update_args['autoload'](which is redefined only if not$autoload !== null) completely and don't pass the updated value to->update()(that's why current code can't swtich fromnotoyes).In short, you need to always pass the boolean no matter what, instead of a
null.jtsternberg commentedon Feb 22, 2018
Ok, good feedback, thank you. Updated. Please test again?
slaFFik commentedon Feb 22, 2018
Looks good, tests for both filter and param for all scenarios passed fine when
true/falseis used.There will be a problem with
'no'string, but I'm not sure something should be done here, at your discretion. Core is explicitly check'no'andfalse, but we (as developers) want strict types :)Thank you for this feature!
Allow also using "no" for autoload option to more closely match the s…