How To Enable Wordpress WP_DEBUG Conditionally

How To Enable WordPress WP_DEBUG Conditionally

Have you got yourself into a situation where you can’t just change WP_DEBUG ( while enabled it displays any errors caused by any PHP code) to true because maybe the site is currently live or you don’t want to show DEBUG info to your team members (because sometimes enabling DEBUGGING break the site layout). Whatever the reason is i have got a solution for you where you can enable WP_DEBUG and disable it based on your custom conditional situation. Let’s Dive in then….

1. Enable WP_DEBUG Only For A Specific IP Address

First let’s enable our debugging for only a specif IP Address ( IP address could be your’s or your client’s, maybe team mate’s… it doesn’t matter. All you have to do is copy that targeted IP Address ).

Question? How to find my IP Address??

You can find your own IP Address using varies methods. Most easy and not so complicated one is using Google Service! Go to Google and Type ‘My IP’. See below Images…Googling My IP - How to Find my IP Address

Now hit enter…

my ip address

Here you can find your IP Address and copy it somewhere to use it later..

Also there are some websites who provides services to find your IP Address. Some to mention are…

And there are many mores not to mention though, where you can find your IP Address.

Using Below Code Snippet, You can enable WP_DEBUG only for that IP..

$MyIP = '43.224.000.00'; //change this IP with your one
if( $_SERVER['REMOTE_ADDR'] !== $MyIP ){ //if it is my IP then enable WP DEBUG
   define( 'WP_DEBUG', true );
}else{ //disable WP_DEBUG for others
   define( 'WP_DEBUG', false );
}

2. Enable WP_DEBUG Using $_GET ( URL GET Parameter ) Only

You can also enable WP_DEBUG when url has ?debug=true in it. Like https://ninjatuts.com/?debug=true, although you can use any secret word instead of debug if needed. Copy below snippet and change debug word if you want to…

debug-using-get-param

$secret_word = 'debug';
if( isset( $_GET[$secret_word] && $_GET[$secret_word] == 'true' ) ){ // you can also change secret word value instead of true 
   define( 'WP_DEBUG', true );
}else{ //disable WP_DEBUG for others
   define( 'WP_DEBUG', false );
}

3. Enable WP_DEBUG only for Specific Browser

You can also enable WP_DEBUG only for a specific Browser like Chrome, Firefox or Safari maybe!! To get what browser currently being used to view the page you can use one of below methods….

  1. How To Get Exact Browser Name And Version – Stackoverflow
  2. Using PHP’s built-in get_browser() function get_browser()

Note when using get_browser() function ( PHP Official Note ):

In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here.

I am using Built-in function for this tutorial here…

$browser = get_browser(null, true ); // it returns an array with browser information
if( $browser['browser'] == 'Firefox'){ //if the browser is Firefox then enable WP DEBUG
   define( 'WP_DEBUG', true );
}else{ //disable WP_DEBUG for others
   define( 'WP_DEBUG', false );
}

Hope! You found your solution. Also if you need any custom other way to enable or disable WP_DEBUG for your site? Let me know in the comments below with what should be the condition…

Leave a Comment

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