Last updated on September 14, 2023

WordPress Hide Deprecated Warnings

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Secure your site by hiding warnings in WordPress.

The code snippet below can be used to hide deprecated warnings in WordPress. This can be useful when you have deprecated functions or features in your code that you don’t want to display warnings for.

// Hide deprecated warnings
function wpsnippets_hide_deprecated_warnings() {
    error_reporting(E_ALL ^ E_DEPRECATED);
}
add_action('init', 'wpsnippets_hide_deprecated_warnings');

By using the error_reporting() function, we can modify the error reporting level to exclude the E_DEPRECATED constant. This will prevent deprecated warnings from being displayed.

The code snippet above creates a custom function wpsnippets_hide_deprecated_warnings() which is hooked to the init action using add_action(). Inside the function, we set the error reporting level to exclude E_DEPRECATED using error_reporting().

This code snippet can be added to your theme’s functions.php file or in a custom plugin file. Once added, it will hide deprecated warnings throughout your WordPress site.

Examples

Example 1: Hiding deprecated warnings using the WP_DEBUG constant

This use case demonstrates how to hide deprecated warnings in WordPress by setting the WP_DEBUG constant to false in the wp-config.php file.

define( 'WP_DEBUG', false );

By setting WP_DEBUG to false, deprecated warnings will no longer be displayed on your website. This is useful when you want to suppress these warnings in a production environment.

Example 2: Hiding deprecated warnings using the error_reporting function

This use case shows how to hide deprecated warnings by modifying the error reporting level using the error_reporting function.

error_reporting( E_ALL & ~E_DEPRECATED );

By using the bitwise operator & and the ~ (tilde) symbol, we can exclude the E_DEPRECATED error level from the error reporting. This effectively hides the deprecated warnings.

Example 3: Hiding deprecated warnings using the deprecated_function_run filter

This use case demonstrates how to hide deprecated warnings by filtering the deprecated_function_run hook and preventing the warnings from being displayed.

add_filter( 'deprecated_function_run', '__return_false' );

By returning false in the filter callback function, the deprecated warnings will be suppressed. This approach gives you more control over which deprecated warnings are hidden, as you can selectively filter them based on the function being called.

Last updated on September 14, 2023. Originally posted on September 16, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *