How to define WP_DEBUG as true outside of wp-config.php?

I am trying to create a debugging mode in one of my plugins so users can easily enable WP_DEBUG on their own, and hopefully report back to us with helpful error messages. Right now we ask them to modify their wp-config.php, but that’s beyond some users’ capabilities. I was hoping to just add define( 'WP_DEBUG', true );in our plugin if the user has checked that option, but it appears you can only enable WP_DEBUG from the wp-config.php file.

Does anyone know of any way to enable WP_DEBUG from anywhere else but wp-config? Or is there another useful WP function that I could be using instead?

Read More

Thanks,
Dalton

Related posts

Leave a Reply

5 comments

  1. WordPress logic forces WP_DEBUG to be defined to something, even if it’s omitted it will be set to false in wp_initial_constants() during load.

    However “background” (that is not when it is checked explicitly) function of WP_DEBUG is to be a flag for how PHP error reporting should be configured on runtime. That configuration is performed by wp_debug_mode() and at any point after that can be changed by your plugin’s code if necessary.

  2. It is not possible to turn on WP_DEBUG because it’s defined in wp-config.php by default, redefinition of defined constants is not possible in PHP.

    If you want to keep them out of wp-config.php ask them to add to the top something like:

    if ( file_exists( 'safe-wp-config.php' ) ) {
        /* this will contains WP_DEBUG */
        include 'safe-wp-config.php';
    }
    

    Alternatively,

    WP_DEBUG is assumed to be false when missing, so let them remove WP_DEBUG completely from wp-config.php and use wherever/whenever they like.

  3. If you do not have access to wp-config.php but you do have access to the theme (or plugin) editor, you can add a code snippet to essentially do the same thing as the WP_DEBUG constant. The wp_debug_mode() function uses the value of this and a few other constants to set the display and log error functions in PHP. You can run the same PHP functions directly with actually touching the WP_DEBUG constant or the wp-config.php file.

    Here are the key PHP functions your code snippet can use:

    • error_reporting( E_ALL ) – sets PHP to display all errors, warnings, and notices.
    • ini_set( 'display_errors', 1 ) – sets PHP to display errors on the screen; use 0 to supress (although if debugging is not enabled in wp-config.php this will already be 0 so you could omit it altogether).
    • ini_set( 'log_errors', 1) – sets PHP to log errors. Like the value above, this can be omitted if you’re not going to log errors. If you do log errors, you’ll need an error log you can get to. The default set by wp_debug_mode() is going to be inaccessible. My example will set it as a text file in the theme directory so you can read it with the theme editor.
    • ini_set( 'error_log', $log_path ) – sets the location of the error log (mentioned above).

    Here’s the code snippet:

    add_action( 'template_redirect', 'my_enable_debug_mode' );
    function my_enable_debug_mode() {
    
        // Turn on error reporting.
        error_reporting( E_ALL );
    
        // Sets to display errors on screen. Use 0 to turn off.
        ini_set( 'display_errors', 1 );
    
        // Sets to log errors. Use 0 (or omit) to not log errors.
        ini_set( 'log_errors', 1 );
    
        // Sets a log file path you can access in the theme editor.
        $log_path = get_template_directory() . '/debug.txt';
        ini_set( 'error_log', $log_path );
    
    }
    

    To summarize, if you need to debug, do not have access to wp-config.php but do have access to the theme editor, you can add this code snippet to the functions.php file to enable debugging on screen along with a txt log file in the theme folder.

  4. However, if you really want to display errors and not store them in log file (like I wanted), then you can turn on debugging for your Public IP like following –

    if ( $_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx')
    define('WP_DEBUG', true);
    

    There should not be a problem as long as you are on Static IP, but if you have dynamic IP, you probably can change the IP every time you need to turn on debugigng.