Skip to content

Instantly share code, notes, and snippets.

@wesbos
Forked from trepmal/toggle-debug.php
Created January 5, 2016 19:18
Show Gist options
  • Save wesbos/e0530e1a413b86943d0d to your computer and use it in GitHub Desktop.
Save wesbos/e0530e1a413b86943d0d to your computer and use it in GitHub Desktop.
WordPress experiment. Toggle debug from admin bar
<?php
/*
Plugin Name: Toggle Debug
Description: Proof-of-concept for an admin-bar debug mode toggle. Needs some UX love.
*/
/*
// In wp-config.php, wrap debug constants in a cookie conditional
if ( isset( $_COOKIE['wp-debug'] ) && $_COOKIE['wp-debug'] == 'on' ) {
define('WP_DEBUG', true);
}
if ( isset( $_COOKIE['script-debug'] ) && $_COOKIE['script-debug'] == 'on' ) {
define('SCRIPT_DEBUG', true);
}
*/
$toggle_debug = new Toggle_Debug();
class Toggle_Debug {
function __construct( ) {
add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 );
}
function init() {
foreach ( array( 'wp', 'script' ) as $type ) {
if ( isset( $_GET['toggle-'. $type .'-debug'] ) ) {
if ( 'on' == $_GET['toggle-'. $type .'-debug'] ) {
//turn off
setcookie( $type. '-debug', '', time()-3600);
unset( $_COOKIE[ $type. '-debug' ] );
}
if ( 'off' == $_GET['toggle-'. $type .'-debug'] ) {
//turn on
setcookie( $type. '-debug', 'on', time()+(60*60*24*30), '/');
}
wp_redirect( remove_query_arg( 'toggle-'. $type .'-debug' ) );
exit;
}
}
}
function admin_bar_menu( $wp_admin_bar ) {
$wp_admin_bar->add_menu( array(
'id' => 'debug-mode',
'title' => 'Debug Mode',
) );
$wp_admin_bar->add_menu( array(
'parent' => 'debug-mode',
'id' => 'debug-mode-wp',
// labeling is ambiguous and should be clarified. describes current status in both title and link
// thus the action in the link has to be inverted ( see init(), if it says 'on' we turn it off...)
'title' => __( 'WP_DEBUG: ' . ( WP_DEBUG ? 'on' : 'off' ) ),
'href' => add_query_arg( 'toggle-wp-debug', ( WP_DEBUG ? 'on' : 'off' ) ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'debug-mode',
'id' => 'debug-mode-script',
'title' => __( 'SCRIPT_DEBUG: ' . ( SCRIPT_DEBUG ? 'on' : 'off' ) ),
'href' => add_query_arg( 'toggle-script-debug', ( SCRIPT_DEBUG ? 'on' : 'off' ) ),
) );
}
}
//eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment