Options, Meta APIs: Add a new `pre-option` filter.

Although a `pre_option_{$option}` filter already exists, this change adds a more general `pre_option` filter that will run on every `get_option` call.  This brings the control flow into similar flow as `update_option`.

Props flixos90, NathanAtmoz, desrosj, spacedmonkey, pbearne.
Fixes #37930.
Built from https://develop.svn.wordpress.org/trunk@54145


git-svn-id: http://core.svn.wordpress.org/trunk@53704 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
davidbaumwald 2022-09-13 19:29:12 +00:00
parent 2608eae664
commit 11a582b0d4
2 changed files with 19 additions and 1 deletions

View File

@ -131,6 +131,24 @@ function get_option( $option, $default = false ) {
*/ */
$pre = apply_filters( "pre_option_{$option}", false, $option, $default ); $pre = apply_filters( "pre_option_{$option}", false, $option, $default );
/**
* Filters the value of all existing options before it is retrieved.
*
* Returning a truthy value from the filter will effectively short-circuit retrieval
* and return the passed value instead.
*
* @since 6.1.0
*
* @param mixed $pre_option The value to return instead of the option value. This differs
* from `$default`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_option().
* Default false (to skip past the short-circuit).
* @param string $option Name of the option.
* @param mixed $default The fallback value to return if the option does not exist.
* Default false.
*/
$pre = apply_filters( 'pre_option', $pre, $option, $default );
if ( false !== $pre ) { if ( false !== $pre ) {
return $pre; return $pre;
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-alpha-54144'; $wp_version = '6.1-alpha-54145';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.