Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
define_wp_debug: Dynamically enable/disable the display of PHP errors in WordPress.
<?php
/**
* Dynamically enable/disable the display of PHP errors in WordPress.
*
* Installation:
* replace line 'define( WP_DEBUG, false );' in 'wp-config.php' file with this code.
*
* Enabling debug mode:
* NOTE: Strongly recommended to changing the 'debug' word to something more unique!
* add the 'debug' query parameter to the URL. Examples:
* https://site.com/?debug - default enabling of WP_DEBUG constant
* https://site.com/?debug=1 - logging of errors into file 'DOCUMENT_ROOT/../php-errors-{HOST}.log'.
* https://site.com/?debug=2 - linking uncompressed scripts and saving all SQL queries to $wpdb->queries
* https://site.com/?debug=3 - saving all SQL queries in $wpdb->queries
* https://site.com/?debug=4 - disable displaying errors (enabled by default)
* https://site.com/?debug=14 - combining
*
* Disabling debug mode:
* https://site.com/?debug=anything
*
* @author Kama (http://wp-kama.ru)
* ver 2.5
*/
// IMPORTANT: change from `debug` to your unique key!
kama_define_wp_debug( 'debug' );
function kama_define_wp_debug( $key ){
$val = isset( $_GET[ $key ] ) ? ( $_GET[ $key ] ?: 'yes' ) : false;
// set/delete cookie
if( $val !== false ){
$cookie = preg_match( '/^(yes|[1234])$/', $val ) ? $val : null;
$host = str_replace( 'www.', '', $_SERVER['HTTP_HOST'] );
// cirilic domains: .сайт, .онлайн, .дети, .ком, .орг, .рус, .укр, .москва, .испытание, .бг
false !== strpos( $host, 'xn--' )
? preg_match( '~xn--[^.]+.xn--[^.]+$~', $host, $mm )
: preg_match( '~[a-z0-9][a-z0-9-]{1,63}.[a-z.]{2,6}$~', $host, $mm );
$host = $mm[0];
$_COOKIE[ $key ] = $cookie;
setcookie( $key, $cookie, time() + ( $cookie ? 3600 * 24 * 365 : -3600 ), '/', ".$host" );
}
// enable the debug based on the cookie
if( ! empty( $_COOKIE[ $key ] ) ){
define( 'WP_DEBUG', true );
$set = array_flip(
preg_split( '/(\d)/', $_COOKIE[ $key ], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY )
);
isset( $set[1] ) && define( 'WP_DEBUG_LOG', dirname( $_SERVER['DOCUMENT_ROOT'] ) . "/php-errors-{$_SERVER['HTTP_HOST']}.log" );
isset( $set[2] ) && define( 'SCRIPT_DEBUG', true );
isset( $set[3] ) && define( 'SAVEQUERIES', true );
isset( $set[4] ) && define( 'WP_DEBUG_DISPLAY', false );
}
else {
define( 'WP_DEBUG', false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment