Using WordPress ‘deprecated_function_run’ PHP action

The deprecated_function_run WordPress PHP action fires when a deprecated function is called.

Usage

add_action('deprecated_function_run', 'your_custom_function', 10, 3);

function your_custom_function($function_name, $replacement, $version) {
    // your custom code here
}

Parameters

  • $function_name (string) – The deprecated function that was called.
  • $replacement (string) – The function that should have been called instead.
  • $version (string) – The version of WordPress that deprecated the function.

More information

See WordPress Developer Resources: deprecated_function_run

Examples

Log deprecated function calls

Log deprecated function calls to a custom log file.

add_action('deprecated_function_run', 'log_deprecated_function_calls', 10, 3);

function log_deprecated_function_calls($function_name, $replacement, $version) {
    // Create the log message
    $message = "Deprecated function '{$function_name}' called. Use '{$replacement}' instead. Deprecated since WP version {$version}.";

    // Write the message to a custom log file
    error_log($message, 3, '/path/to/custom.log');
}

Display deprecated function calls as admin notices

Show deprecated function calls as admin notices for debugging purposes.

add_action('deprecated_function_run', 'show_deprecated_function_notices', 10, 3);

function show_deprecated_function_notices($function_name, $replacement, $version) {
    // Check if the current user can manage options (usually an administrator)
    if (current_user_can('manage_options')) {
        // Add the admin notice
        add_action('admin_notices', function() use ($function_name, $replacement, $version) {
            echo '<div class="notice notice-warning">';
            echo '<p>';
            echo "Deprecated function '<strong>{$function_name}</strong>' called. Use '<strong>{$replacement}</strong>' instead. Deprecated since WP version <strong>{$version}</strong>.";
            echo '</p>';
            echo '</div>';
        });
    }
}

Email deprecated function calls

Send an email to the administrator when a deprecated function is called.

add_action('deprecated_function_run', 'email_deprecated_function_calls', 10, 3);

function email_deprecated_function_calls($function_name, $replacement, $version) {
    // Create the email subject and message
    $subject = "Deprecated function '{$function_name}' called on your WordPress site";
    $message = "The deprecated function '{$function_name}' was called on your WordPress site. Use '{$replacement}' instead. Deprecated since WP version {$version}.";

    // Send the email to the site administrator
    wp_mail(get_option('admin_email'), $subject, $message);
}

Add a CSS class to the body tag

Add a CSS class to the body tag when a deprecated function is called. Useful for visually identifying pages with deprecated functions during development.

add_action('deprecated_function_run', 'add_body_class_for_deprecated_functions', 10, 3);

function add_body_class_for_deprecated_functions($function_name, $replacement, $version) {
    // Add a filter to the body_class function
    add_filter('body_class', function($classes) {
        $classes[] = 'deprecated-function-used';
        return $classes;
    });
}

Disable deprecated functions in production

Prevent deprecated functions from running in a production environment.

add_action('deprecated_function_run', 'disable_deprecated_functions', 10, 3);

function disable_deprecated_functions($function_name, $replacement, $version) {
    // Check if the current environment is production
    if (defined('WP_ENV') && WP_ENV === 'production') {
        // Show an error message and terminate the script
        wp_die("Deprecated function '{$function_name}' called. Use '{$replacement}' instead. Deprecated since WP version {$version}. Please update your code.");
    }
}