15

When I use:

define('WP_DEBUG', 1);

In my wp-config.php, it works fine, but I am hacking an old theme and I would like to suppress deprecated notices.

My understanding is that adding this:

error_reporting( E_ERROR | E_NOTICE | E_PARSE )

Should do the trick. I have added it to wp-config.php and to header.php in my theme. Unfortunately, it has no effect. Is this something set at server level? Also the following makes no difference as well:

ini_set('display_errors', 1);

As asked in the comments below here's a couple of the notices. I am using a hacked version of the Construct 2 theme, quite old now but it would not be safe to update it. I am trying to persuade the client to let me rewrite it, the site is fairly simple, but as he can't see anything wrong, it's not broken, he won't spend the money.

Deprecated: Assigning the return value of new by reference is deprecated in /Volumes/Macintosh HD/Sites/MAMP (custodian)/wordpress/wp-content/themes/construct2/option-tree/ot-loader.php on line 369

Strict Standards: Declaration of DropDown_Nav_Menu::start_lvl() should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array) in /Volumes/Macintosh HD/Sites/MAMP (custodian)/wordpress/wp-content/themes/construct2/dropdown-menus.php on line 192

4
  • 1
    in which file appears the first notice ?
    – mmm
    Commented Sep 16, 2016 at 10:31
  • May you display the depreciated notice you are getting? This will help me give you a better answer Commented Sep 16, 2016 at 14:04
  • See above added to original post
    – Chris Pink
    Commented Sep 18, 2016 at 9:55
  • @ChrisPink From my experience when working with an older WordPress install, I would put the code I provided in my answer to both the ot-loader.php and dropdown-menus.php file. Commented Sep 18, 2016 at 14:37

5 Answers 5

17

I was able to suppress notices in the log and displayed errors by using the error_reporting function in a must-use plugin, which is loaded early enough to catch most of the WP core code warnings. This is helpful for the warnings coming from core/plugin files that you shouldn't modify.

I created a php file in the /wp-content/mu-plugins/ folder with this code:

<?php 
error_reporting(E_ALL & ~E_WARNING & ~E_DEPRECATED & ~E_USER_DEPRECATED & ~E_NOTICE);
?>
2
  • Thank you, this worked for me!
    – Azamat
    Commented Feb 9, 2022 at 23:21
  • 1
    Thank you so much! I've created a WordPress plugin to control the MU plugin from the comfort of the admin panel. Commented Oct 30, 2023 at 9:16
5

As mmm stated:

in which file appears the first notice?

Wherever the notice is mentioning the location of this deprecated function (path/to/some/file.php), you would insert the following just below the <?php tag which starts off the file:

error_reporting(0);

I've tried the above functions you mentioned and inserted them in my wp-config.php when I experience something similar, but they didn't work for me. This will turn off warning, deprecated, and everything else except the errors.

4
  • Interesting (ie showing the shallowness of my understanding, inserting error_reporting(0); (or any other variation) in the first of the files mentioned in the long list of error messages suppresses all messages from subsequent different files but not the deprecated notice in that file
    – Chris Pink
    Commented Sep 18, 2016 at 10:04
  • @ChrisPink I don't entirely understand your last comment, may you please clarify? Commented Sep 18, 2016 at 14:33
  • Sorry, I didn't explain very well. Inserting error_reporting(0) into the file ot-loader.php (as per message in the OP) suppressed all subsequent notices but not the one originating from ot-loader.php (the one in the OP). So I am guessing I need to insert the error_reporting() function into the file that calls ot-loader.php.
    – Chris Pink
    Commented Sep 20, 2016 at 7:35
  • @ChrisPink Yes, insert that code in ot-loader.php. Commented Sep 20, 2016 at 17:50
5

This is probably the most "WordPress" way to do it.

The code MUST go in your wp-config file, after the WP_DEBUG (and any other) defines because this particular filter runs before any plugins are loaded.

$GLOBALS['wp_filter'] = array(
'enable_wp_debug_mode_checks' => array(
    10 => array(
        array(
            'accepted_args' => 0,
            'function'      => function () {
                if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
                    // *** This is the key line - change to adjust to whatever logging state you want
                    error_reporting( E_ALL & ~E_DEPRECATED );

                    ini_set( 'display_errors', defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ? 1 : 0 );

                    if ( in_array( strtolower( (string) WP_DEBUG_LOG ), array( 'true', '1' ), true ) ) {
                        $log_path = WP_CONTENT_DIR . '/debug.log';
                    } elseif ( is_string( WP_DEBUG_LOG ) ) {
                        $log_path = WP_DEBUG_LOG;
                    } else {
                        $log_path = false;
                    }

                    if ( $log_path ) {
                        ini_set( 'log_errors', 1 );
                        ini_set( 'error_log', $log_path );
                    }

                    if (
                        defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || defined( 'MS_FILES_REQUEST' ) ||
                        ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) ||
                        wp_doing_ajax() || wp_is_json_request() ) {
                        ini_set( 'display_errors', 0 );
                    }
                }

                return false;
            },
        ),
    ),
));
2
  • Why before any plugins are loaded? The deprecated warnings are from the theme.
    – Rup
    Commented Feb 3, 2023 at 12:46
  • This gave me the error " Fatal error: Call to undefined function wp_is_json_request() in /var/www/html/wp-config.php on line 111", commenting that part of the check away did work (note that this is in an old wordpress install)
    – Ferrybig
    Commented Mar 24, 2023 at 8:27
1

it seems many plugins change this value as well, so, i have had good luck with these two approaches,

// trying to be the last person to set this value
add_action('wp_loaded', function() { error_reporting( E_ALL & ~E_NOTICE ); 
});

// even later, but, not too late
add_action('wp', function() { error_reporting( E_ALL & ~E_NOTICE ); 
});

1

With the following you can just remove deprecated from whatever is the current error reporting setting. Adding it to your themes functions.php hides all deprecation errors.

error_reporting(error_reporting() & ~E_DEPRECATED);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.