Skip to content

Instantly share code, notes, and snippets.

@ahmu83
Last active April 23, 2023 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmu83/6d731a028f368a83419cc928a7ff9094 to your computer and use it in GitHub Desktop.
Save ahmu83/6d731a028f368a83419cc928a7ff9094 to your computer and use it in GitHub Desktop.
WP_DEBUG Toggler. Toggle WP_DEBUG using a URL parameter.
<?php
/**
* WP_DEBUG toggle for conveniently turning on and off from the a URL param "debug"
* Its good when working on a staging site where you need debug functionality on/off
*
* usage:
*
* Include this file in wp-config.php (include 'wp-debug.php';)
* Make sure to comment out all these constants in wp-config.php
*
* WP_DEBUG, WP_DEBUG_LOG, WP_DEBUG_DISPLAY, SCRIPT_DEBUG
*
* ?debug=1 will turn on debugging
* ?debug=0 will turn off debugging
*
*/
/**
* Do not run the script if any of these constants is defined
*/
if (
defined('WP_DEBUG') ||
defined('SCRIPT_DEBUG') ||
defined('WP_DEBUG_LOG') ||
defined('WP_DEBUG_DISPLAY')
) {
return;
}
$wp_debug = wp_debug();
// Enable WP_DEBUG mode
define( 'WP_DEBUG', $wp_debug );
// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', $wp_debug );
// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', $wp_debug );
@ini_set( 'display_errors', ($wp_debug ? 1 : 0) );
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', $wp_debug );
/**
* @return boolean
*/
function wp_debug() {
if ( session_status() === PHP_SESSION_NONE ) {
@session_start();
}
$debug_toggle = isset($_GET['debug']) ? (int) $_GET['debug'] : null;
if ($debug_toggle === 1) {
$_SESSION['debug'] = 1;
} else if ($debug_toggle === 0) {
unset($_SESSION['debug']);
}
$debug = isset($_SESSION['debug']) && $_SESSION['debug'] == 1 ? true : false;
$debug = $debug === false ? (isset($_GET['debug']) && (int) $_GET['debug'] === 1 ? true : false) : $debug;
return $debug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment