wppaste
WordPress

_deprecated_argument( string $function_name, string $version, string $message = '' )

Since
3.0.0, 5.4.0, 5.4.0
Source
wp-includes/functions.php:5973

Fires the deprecated_argument_run action for a legacy function argument and raises an E_USER_DEPRECATED notice when WP_DEBUG is enabled. It does not check whether the argument was actually used, the caller must test that first and only call this function when the deprecated value was supplied. Pair it with the deprecated_argument_trigger_error filter to control whether the on-screen notice appears, or use _deprecated_function when an entire function, rather than one of its arguments, is being retired.

Marks a function argument as deprecated and inform when it has been used.

Description

This function is to be used whenever a deprecated function argument is used.
Before this function is called, the argument must be checked for whether it was used by comparing it to its default value or evaluating whether it is empty.

For example:

if ( ! empty( $deprecated ) ) {
 _deprecated_argument( __FUNCTION__, '3.0.0' );
}

There is a 'deprecated_argument_run' hook that will be called that can be used to get the backtrace up to what file and function used the deprecated argument.

The current behavior is to trigger a user error if WP_DEBUG is true.

Compatibility

WordPress
since 5.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$function_namestring
The function that was called.
$versionstring
The version of WordPress that deprecated the argument used.
$messagestringoptional
A message regarding the change. Default empty string.Default: ''

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Warn developers when a legacy argument is still being passed to a custom function

A shop helper used to accept a raw meta key as its second argument and now ignores it, so it reports the change whenever that argument is still supplied.

add_action( 'deprecated_argument_run', function( $function_name, $message, $version ) {
	printf( 'Deprecated argument in %s() since %s: %s', esc_html( $function_name ), esc_html( $version ), esc_html( $message ) );
}, 10, 3 );

function shop_get_product_price( $post_id, $legacy_meta_key = null ) {
	if ( ! empty( $legacy_meta_key ) ) {
		_deprecated_argument( __FUNCTION__, '5.4.0', 'The meta key is now fixed to "price" and no longer configurable.' );
	}

	return get_post_meta( $post_id, 'price', true );
}

echo '<p>Price: ' . esc_html( shop_get_product_price( 2, 'price' ) ) . '</p>';

The empty() check on $legacy_meta_key happens before the call, which is the pattern _deprecated_argument expects every caller to follow.

Suppress the on-screen deprecation notice while still logging it

Some hosts run with WP_DEBUG off in production, so the deprecated_argument_run action is used to capture the message instead of relying on the notice.

add_filter( 'deprecated_argument_trigger_error', '__return_false' );

add_action( 'deprecated_argument_run', function( $function_name, $message, $version ) {
	echo '<p>Logged: ' . esc_html( $function_name ) . ' (' . esc_html( $version ) . ') - ' . esc_html( $message ) . '</p>';
} );

_deprecated_argument( 'legacy_widget_render', '3.0.0', 'Pass widget options as an array, not as separate arguments.' );

echo '<p>Notice suppressed but the action still ran.</p>';

Returning false from deprecated_argument_trigger_error stops the E_USER_DEPRECATED trigger even when WP_DEBUG is true, but the action fires regardless of that filter.

Common problems and fixes · 3

Why does _deprecated_argument not display anything on my site?

The function only calls wp_trigger_error() when WP_DEBUG is true and the deprecated_argument_trigger_error filter also returns true, so on a production site with debugging off nothing shows on screen. - Enable WP_DEBUG in wp-config.php to see the notice. - Or hook deprecated_argument_run directly to capture the message regardless of WP_DEBUG.

Do I need to check the argument's value before calling this function?

Yes. The function itself never inspects the argument, it just logs a message and optionally triggers an error, so calling it unconditionally would report every call as deprecated, even ones that never used the old argument. `` if ( ! empty( $deprecated_argument ) ) { _deprecated_argument( __FUNCTION__, '5.4.0' ); } ``

How do I include the actual deprecated value in the notice?

The $message parameter is a plain string built by the caller, the function does not interpolate the argument's value for you. Build the string yourself before passing it, for example with sprintf(), so the notice tells developers what value triggered it.

Alternatives and related functions

_deprecated_function
When an entire function or method is being phased out rather than just one of its parameters.
_deprecated_hook
When an action or filter hook itself is deprecated instead of an argument passed to a function.
_deprecated_constructor
When a class's old-style PHP4 constructor is deprecated in favor of __construct().
_doing_it_wrong
When the code is being misused in a way that is not simply a deprecated argument, such as calling it too early or with an invalid combination of values.

Performance profile

How much work a call to _deprecated_argument() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–38

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.

Plugin surface
2 hooks

Third-party callbacks on 'deprecated_argument_run', 'deprecated_argument_trigger_error' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _deprecated_argument() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always12do_action()
!apply_filters()17do_action(), apply_filters()
apply_filters() && !function_exists()33–35do_action(), apply_filters(), function_exists(), sprintf(), wp_trigger_error()
apply_filters() && function_exists()37–38do_action(), apply_filters(), function_exists(), __(), sprintf(), wp_trigger_error()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 63 instructions, 12–38 executed per call, 5 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while _deprecated_argument() runs, in this order:

  1. do_action( deprecated_argument_run )actionline 5984 (+11 into the body)

    Fires when a deprecated argument is called.

  2. apply_filters( deprecated_argument_trigger_error )filterline 5993 (+20 into the body)

    Filters whether to trigger an error for deprecated arguments.

Uses · 4

  • do_action()Calls the callback functions that have been added to an action hook.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • __()Retrieves the translation of $text.
  • wp_trigger_error()Generates a user-level error/warning/notice/deprecation message.

Used by · 50

Show all 50

Source code

function _deprecated_argument( $function_name, $version, $message = '' ) { 	/**	 * Fires when a deprecated argument is called.	 *	 * @since 3.0.0	 *	 * @param string $function_name The function that was called.	 * @param string $message       A message regarding the change.	 * @param string $version       The version of WordPress that deprecated the argument used.	 */	do_action( 'deprecated_argument_run', $function_name, $message, $version ); 	/**	 * Filters whether to trigger an error for deprecated arguments.	 *	 * @since 3.0.0	 *	 * @param bool $trigger Whether to trigger the error for deprecated arguments. Default true.	 */	if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) {		if ( function_exists( '__' ) ) {			if ( $message ) {				$message = sprintf(					/* translators: 1: PHP function name, 2: Version number, 3: Optional message regarding the change. */					__( 'Function %1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s' ),					$function_name,					$version,					$message				);			} else {				$message = sprintf(					/* translators: 1: PHP function name, 2: Version number. */					__( 'Function %1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.' ),					$function_name,					$version				);			}		} else {			if ( $message ) {				$message = sprintf(					'Function %1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s',					$function_name,					$version,					$message				);			} else {				$message = sprintf(					'Function %1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.',					$function_name,					$version				);			}		} 		wp_trigger_error( '', $message, E_USER_DEPRECATED );	}}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.4.0
The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE).from the docblock
5.4.0
This function is no longer marked as "private".from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.