#38176 closed enhancement (fixed)
Add 'default' to register_setting
Reported by: | rmccue | Owned by: | joehoyle |
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Options, Meta APIs | Keywords: | has-patch dev-feedback needs-refresh |
Focuses: | Cc: |
Description
Imagine a future where you don't need to pass your default settings every time you call get_option
. Instead, you simply pass into register_setting
once.
Attached patch does this. Also moves the register_setting
group of functions from wp-admin
to wp-includes/option.php
to allow usage everywhere, rather than just in the admin. This is probably a Good Idea with the changes made in #37885, as register_setting
is now also useful for the REST API, not just the admin.
This does do some funky meta-programming that sucks a bit: if you call get_option( 'option_name' )
without a default parameter, it uses the registered default. If you call get_option( 'option_name', false )
, it uses the value you passed in (false
) even though this is the default parameter value. This isn't great, but allows us to do this nicely with backwards compatibility and is somewhat less surprising.
Attachments (5)
Change History (32)
#2
@
8 years ago
This looks good to me, +1 on the moving of the function too - right now I'm having to manually include the admin file when we want to use register_setting
outside of the admin, which is fairly common now with the additions.
This ticket was mentioned in Slack in #core by rmccue. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by rmccue. View the logs.
8 years ago
#8
@
8 years ago
- Keywords needs-refresh added
With the moving of the functions done in [38687], this needs a refresh.
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by desrosj. View the logs.
8 years ago
#12
@
8 years ago
I somewhat interesting and controversial idea suggested by @jorbin is to _deprecate_ the $default
argument for get_option()
and instead recommend the use of register_setting
, as in (I think) virtually all cases, one would want a consistent default for their options, and it leads to developer error by having to re-specify the default on every get_option
call.
I think this would be a pretty good idea, however is a small but wide impacting change, and I'm not sure that going this far is required just to support the default
argument in register_setting
. How about we start with adding it to register_setting
but leave the deprecation of default
in get_option
for down the road?
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by jorbin. View the logs.
8 years ago
#18
@
8 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
The default_option_
filter in update_option()
does not pass a third parameter, which the filter_default_option()
function requires. See https://travis-ci.org/aaronjorbin/develop.wordpress/builds/170519570
#19
@
8 years ago
38176.5.diff adds false
as the third parameter when applying the default_option_
filter in update_option()
. This seems like the right assumption to make here.
This resolves the errors in the tests.
As an example of how this works: