#37930 closed enhancement (fixed)
Introduce a general `pre_option` filter in `get_option()`
Reported by: | flixos90 | Owned by: | davidbaumwald |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Options, Meta APIs | Keywords: | has-patch has-unit-tests commit needs-dev-note |
Focuses: | Cc: |
Description
We currently have a pre_option_{$option}
filter in get_option()
to short-circuit the process of retrieving an option. I think it would make sense to also add a global filter pre_option
, similar like we have in update_option()
.
Attachments (3)
Change History (32)
#2
@
8 years ago
I think this makes sense. I just have a question about the order.
The get_option() function filters pre_update_option_{$option}
before pre_update_option
, so would it make more sense to keep the same order in get_option()?
This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.
8 years ago
#4
@
8 years ago
Made an adjustment on the patch. The filter above it checks for false !== $value
where the first patch checked for ! is_null()
. Changed that for consistency.
Also, updated the PHPDocblock to reflect 4.8.0
, and fix a typo.
#5
@
8 years ago
I looked at this one first of the three, of course (#37929 and #37928), and the other two check for null
.
I did some digging because I feel like most of the "short circuit" filters I have used in core have a truthy check for false !== $value
instead of null
. It seems there is a pretty equal number throughout core for each. Here are some examples.
Truthy check
- Shortcodes: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/shortcodes.php#L338
pre_get_lastpostmodified
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L5571pre_option_{$option}
above current new filter.pre_site_option_{$option}
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/option.php#L1124pre_site_transient_{$transient}
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/option.php#L1594pre_transient_{$transient}
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/option.php#L641- When retrieving
WP_Cache
items this is appropriate.
Locations using a null !== $value
check:
wp_save_image_editor_file
: https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/image-edit.php#L317wp_save_image_file
: https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/image-edit.php#L344- 'pre_get_table_charset': https://core.trac.wordpress.org/browser/trunk/src/wp-includes/wp-db.php#L2451
pre_get_blogs_of_user
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/user.php?rev=37326#L594pre_delete_post
: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L2446
I think that we should at least remain consistent in the options API.
Looking through options.php
, I see the following:
Truthy checks already in core:
pre_site_transient_{$transient}
pre_option_{$option}
pre_get_option
- addressed in this patchpre_transient_{$transient}
,pre_site_option_{$option}
There are currently only two null !== $value
or ! is_null()
checks in options.php
, and both follow database queries that more than likely return null
.
I vote for false !==
checks, unless there are reasons why null
is beter that I don't know of.
This ticket was mentioned in Slack in #core-multisite by desrosj. View the logs.
8 years ago
#7
@
7 years ago
Updated this patch with another solution. Consider the following code in update_option
$value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );
/**
* Filters an option before its value is (maybe) serialized and updated.
*
* @since 3.9.0
*
* @param mixed $value The new, unserialized option value.
* @param string $option Name of the option.
* @param mixed $old_value The old option value.
*/
$value = apply_filters( 'pre_update_option', $value, $option, $old_value );
It passes the value through both filters. I have updated my patch so follow this patten. My patch passes the variable through the pre_get_option second and uses the existing return.
This ticket was mentioned in Slack in #core-multisite by spacedmonkey. View the logs.
7 years ago
#10
@
7 years ago
I'd like to also see general filters implemented for default_option
in get_option()
as well, used in three potential places for return there.
This ticket was mentioned in PR #2859 on WordPress/wordpress-develop by pbearne.
2 years ago
#13
- Keywords has-unit-tests added
Reash and added test
Trac ticket: https://core.trac.wordpress.org/ticket/37930
This ticket was mentioned in Slack in #core by pbearne. View the logs.
2 years ago
This ticket was mentioned in Slack in #core by pbearne. View the logs.
2 years ago
#19
@
2 years ago
- Keywords commit added
@spacedmonkey Is this still on your list to commit before 6.1 Beta 1? I am happy to commit this around the same time as #56045, since they are very similar.
dream-encode commented on PR #2859:
2 years ago
#22
Merged into core in https://core.trac.wordpress.org/changeset/54145.
#24
@
2 years ago
- Keywords needs-dev-note add-to-field-guide added
This one can either go into grouped Dev note or just in Field guide.
37930.diff adds a filter
pre_get_option
. It works similar like the new filters proposed in #37928 and #37929 respectively.