_deprecated_function( string $function_name, string $version, string $replacement = '' )
- Since
- 2.5.0, 5.4.0, 5.4.0
- Source
wp-includes/functions.php:5524
Flags $function_name as deprecated since $version and optionally points to a $replacement function, without calling that replacement for you. It only surfaces a visible notice when WP_DEBUG is true, but it always fires the deprecated_function_run action, so plugins can log deprecated calls even in production. Pair it with the deprecated_function_trigger_error filter if you need to silence or redirect the on-screen warning.
Description
There is a 'deprecated_function_run' hook that will be called that can be used to get the backtrace up to what file and function called the deprecated function.
The current behavior is to trigger a user error if WP_DEBUG is true.
This function is to be used in every function that is deprecated.
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 function.
$replacementstringoptional- The function that should have been called. 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.
Mark a plugin function as deprecated and fall back to a replacement
A plugin renaming a helper keeps the old name working while flagging it as deprecated.
function acme_get_product_price( $post_id ) {
_deprecated_function( __FUNCTION__, '7.1.0', 'get_post_meta()' );
return get_post_meta( $post_id, 'price', true );
}
$price = acme_get_product_price( 2 );
echo esc_html( 'Price for post 2 via deprecated helper: ' . $price );The function only produces an on-screen PHP notice when WP_DEBUG is true; the fallback call to get_post_meta() still runs either way.
Log every deprecated function call instead of showing a warning
Hook the deprecated_function_run action to capture deprecated calls for your own audit log rather than relying on the default error output.
add_action( 'deprecated_function_run', function( $function_name, $replacement, $version ) {
printf(
'Logged deprecated call: %s (deprecated in %s, replacement: %s)',
esc_html( $function_name ),
esc_html( $version ),
esc_html( $replacement ? $replacement : 'none' )
);
} );
function acme_legacy_json_encode( $data ) {
_deprecated_function( __FUNCTION__, '7.1.0', 'wp_json_encode()' );
return wp_json_encode( $data );
}
$json = acme_legacy_json_encode( array( 'post_id' => 1 ) );
echo '<br>Encoded: ' . esc_html( $json );do_action( 'deprecated_function_run' ) fires on every call regardless of WP_DEBUG, so this is the reliable way to track deprecated usage in production.
Common problems and fixes · 4
- Why doesn't calling _deprecated_function() show any warning on my site?
- Does _deprecated_function() automatically call the replacement function for me?
- How do I silence deprecated-function notices from a third-party plugin without editing its code?
- Can I still detect deprecated calls when WP_DEBUG is off in production?
Why doesn't calling _deprecated_function() show any warning on my site?
Does _deprecated_function() automatically call the replacement function for me?
How do I silence deprecated-function notices from a third-party plugin without editing its code?
Can I still detect deprecated calls when WP_DEBUG is off in production?
Alternatives and related functions
_deprecated_argument- When a function or method still exists but one of its parameters or an accepted value has been dropped.
_deprecated_class- When an entire class, rather than a single function, has been replaced or removed.
_deprecated_hook- When a filter or action itself is being retired instead of a callable function.
_doing_it_wrong- When the function is still current but is being called in an incorrect way, rather than being obsolete.
Performance profile
How much work a call to _deprecated_function() 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
- Scaling
- Constant
- Instructions
- 12–38
- Plugin surface
- 2 hooks
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.
Third-party callbacks on 'deprecated_function_run', 'deprecated_function_trigger_error' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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_function() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12 | do_action() |
!apply_filters() | 17 | do_action(), apply_filters() |
apply_filters() && !function_exists() | 33–35 | do_action(), apply_filters(), function_exists(), sprintf(), wp_trigger_error() |
apply_filters() && function_exists() | 37–38 | do_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_function() runs, in this order:
- do_action( deprecated_function_run )actionline 5535 (+11 into the body)
Fires when a deprecated function is called.
- apply_filters( deprecated_function_trigger_error )filterline 5544 (+20 into the body)
Filters whether to trigger an error for deprecated functions.
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
- Custom_Image_Header::create_attachment_object()Creates an attachment 'object'.
- Services_JSON::__construct()constructs a new JSON instance
- Services_JSON::_encode()PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
- Services_JSON::decode()decodes a JSON string into appropriate variable
- Services_JSON::encode()encodes an arbitrary variable into JSON format (and sends JSON Header)
- Services_JSON::encodeUnsafe()encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
- Services_JSON::isError()
- Services_JSON::name_value()array-walking function for use in generating JSON-formatted name-value pairs
- Services_JSON::reduce_string()reduce a string by removing leading and trailing comments and whitespace
- Services_JSON::strlen8()Calculates length of string in bytes
- Services_JSON::substr8()Returns part of a string, interpreting $start and $length as number of bytes.
- Services_JSON::utf162utf8()convert a string from one UTF-16 char to one UTF-8 char
Show all 50
- Services_JSON::utf82utf16()convert a string from one UTF-8 char to one UTF-16 char
- Services_JSON_Error::__construct()PHP5 constructor.
- WP_Admin_Bar::recursive_render()Renders toolbar items recursively.
- WP_Community_Events::format_event_data_time()Adds formatted date and time items for each event in an API response.
- WP_Community_Events::maybe_log_events_response()Logs responses to Events API requests.
- WP_Customize_Image_Control::add_tab()
- WP_Customize_Image_Control::print_tab_image()
- WP_Customize_Image_Control::remove_tab()
- WP_Customize_Manager::_cmp_priority()Helper function to compare two objects by priority, ensuring sort stability via instance_number.
- WP_Customize_Manager::customize_preview_base()Prints base element for preview frame.
- WP_Customize_Manager::customize_preview_html5()Prints a workaround to handle HTML5 tags in IE < 9.
- WP_Customize_Manager::customize_preview_override_404_status()Prevents sending a 404 status when returning the response for the customize preview, since it causes the jQuery Ajax to fail. Send 200 instead.
- WP_Customize_Manager::customize_preview_signature()Prints a signature so we can ensure the Customizer was properly executed.
- WP_Customize_Manager::remove_preview_signature()Removes the signature in case we experience a case where the Customizer was not properly executed.
- WP_Customize_Manager::wp_die_handler()Returns the Ajax wp_die() handler if it's a customized request.
- WP_Customize_Manager::wp_redirect_status()Prevents Ajax requests from following redirects when previewing a theme by issuing a 200 response instead of a 30x.
- WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby()Sort menu objects by the class-supplied orderby property.
- WP_Customize_Nav_Menus_Panel::wp_nav_menu_manage_columns()Returns the advanced options for the nav menus page.
- WP_Customize_New_Menu_Control::__construct()Constructor.
- WP_Customize_New_Menu_Control::render_content()Render the control's content.
- WP_Customize_New_Menu_Section::__construct()Constructor.
- WP_Customize_New_Menu_Section::render()Render the section, and the controls that have been added to it.
- WP_Customize_Setting::_update_option()Deprecated method.
- WP_Customize_Setting::_update_theme_mod()Deprecated method.
- WP_Customize_Widgets::prepreview_added_sidebars_widgets(){@internal Missing Summary}
- WP_Customize_Widgets::prepreview_added_widget_instance(){@internal Missing Summary}
- WP_Customize_Widgets::remove_prepreview_filters(){@internal Missing Summary}
- WP_Customize_Widgets::setup_widget_addition_previews(){@internal Missing Summary}
- WP_Duotone::get_filter_css_property_value_from_preset()Gets the CSS filter property value from a preset.
- WP_Duotone::get_filter_id_from_preset()Returns the prefixed id for the duotone filter for use as a CSS id.
- WP_Duotone::get_filter_svg_from_preset()Gets the SVG for the duotone filter definition from a preset.
- WP_Filesystem_Base::find_base_dir()Locates a folder on the remote filesystem.
- WP_Filesystem_Base::get_base_dir()Locates a folder on the remote filesystem.
- WP_Http::parse_url()Used as a wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7.
- WP_Image_Editor_Imagick::set_imagick_time_limit()Sets Imagick time limit.
- WP_Interactivity_API::print_client_interactivity_data()Prints the serialized client-side interactivity data.
- WP_Interactivity_API::print_router_loading_and_screen_reader_markup()Deprecated.
- WP_Interactivity_API::register_script_modules()Registers the `@wordpress/interactivity` script modules.
Source code
function _deprecated_function( $function_name, $version, $replacement = '' ) { /** * Fires when a deprecated function is called. * * @since 2.5.0 * * @param string $function_name The function that was called. * @param string $replacement The function that should have been called. * @param string $version The version of WordPress that deprecated the function. */ do_action( 'deprecated_function_run', $function_name, $replacement, $version ); /** * Filters whether to trigger an error for deprecated functions. * * @since 2.5.0 * * @param bool $trigger Whether to trigger the error for deprecated functions. Default true. */ if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) { if ( function_exists( '__' ) ) { if ( $replacement ) { $message = sprintf( /* translators: 1: PHP function name, 2: Version number, 3: Alternative function name. */ __( 'Function %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $function_name, $version, $replacement ); } else { $message = sprintf( /* translators: 1: PHP function name, 2: Version number. */ __( 'Function %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $function_name, $version ); } } else { if ( $replacement ) { $message = sprintf( 'Function %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function_name, $version, $replacement ); } else { $message = sprintf( 'Function %1$s 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 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.