Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 3 months ago

#41254 closed enhancement (fixed)

Add $default to pre_option_{$option} filter

Reported by: sc0ttkclark Owned by: DrewAPicture
Priority: normal Milestone: 4.9
Component: Options, Meta APIs Version: 4.4
Severity: normal Keywords: has-patch commit
Cc: Focuses:

Description

Currently the pre_option_{$option} filter passes two options:

	/**
	 * Filters the value of an existing option before it is retrieved.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * Passing a truthy value to the filter will short-circuit retrieving
	 * the option value, returning the passed value instead.
	 *
	 * @since 1.5.0
	 * @since 4.4.0 The `$option` parameter was added.
	 *
	 * @param bool|mixed $pre_option Value to return instead of the option value.
	 *                               Default false to skip it.
	 * @param string     $option     Option name.
	 */
	$pre = apply_filters( "pre_option_{$option}", false, $option );

What I believe that is needed here is to add $default as well, which could be valuable to the callback hooking in (assuming $default is not false in those use cases).

Attachments (4)

41254.patch​ (609 bytes ) - added by sc0ttkclark 9 years ago.
41254.2.patch​ (828 bytes ) - added by sc0ttkclark 9 years ago.
41254.3.patch​ (828 bytes ) - added by sc0ttkclark 9 years ago.
41254.png​ (208.5 KB ) - added by ramiy 9 years ago.

Download all attachments as: .zip

Change History (11)

#1 @sc0ttkclark
9 years ago

Example use case:

I want to override certain options (from a plugin) for sites on a multisite install to use an option that is instead network-wide.

Last edited 9 years ago by sc0ttkclark (previous) (diff)

#2 @DrewAPicture
9 years ago

  • Keywords has-patch commit added
  • Milestone Awaiting Review → 4.9

I'm on board with this. Having access to the value of $default here would be useful, and actually also in the pre_site_option_{$option} hook as well.

#3 @DrewAPicture
9 years ago

  • Owner set to DrewAPicture
  • Resolution → fixed
  • Status new → closed

In 41013:

Options: Pass $default as a new parameter to the pre_option_{$option} and pre_site_option_{$option} filters.

This change standardizes some language in the parameter descriptions between the two hook docs in an effort to reduce confusion between the $pre_option pseudo-parameters and the new $default ones. $pre_option is the truthy value used to short-circuit the get_*option() call, $default is the value passed to the get_*option() returned in the event that 1) a short circuit isn't performed, 2) the option doesn't exist.

Props sc0ttkclark.
Fixes #41254.

#4 @johnbillion
9 years ago

In 41617:

Options, Meta APIs: Require a confirmation link in an email to be clicked when a user attempts to change the network
admin email address on Multisite.

This mirrors the same functionality for the site admin email address and user profile email address.

Fixes #41254

This ticket was mentioned in ​Slack in #core by jorbin. ​View the logs.


9 years ago

#7 @westonruter
3 months ago

In 62933:

XML-RPC: Pass the missing args to the enable_xmlrpc filters.

The option_{$option} filter has documented an $option parameter since 4.4.0, when r33738 added it, and pre_option_{$option} has documented both $option and, since 4.9.0, $default_value, added in r41013. However, WP_XMLRPC_Server::set_is_enabled() applies the enable_xmlrpc instances of these filters directly, in order to respect callbacks left over from before the option was deprecated in 3.5.0, and those calls were left passing only the value. A callback declaring the later parameters therefore raised an ArgumentCountError here, even though the very same callback works when the filter is reached through get_option().

The option name and the default are passed explicitly, since there is no $option variable in scope. The default matches what the filter would receive from get_option( 'enable_xmlrpc', false ).

Also add the standard reference comments pointing at the canonical docblocks in wp-includes/option.php.

These call sites were surfaced by the PHPStan extensions under development for this ticket, which are committed separately.

Developed as subset of ​https://github.com/WordPress/wordpress-develop/pull/12022.
Follow-up to r21804, r33738, r41013.

See #65376, #28402, #41254.

Note: See TracTickets for help on using tickets.