Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@wesbos
Copy link

wesbos commented Jan 5, 2016

Line 32 needs a 4th arg of '/' for this to work

@trepmal
Copy link
Author

trepmal commented Jan 5, 2016

A quick test (in case you're on a perfectly functioning site) is to enqueue a script inside init(), that's too early so if debug is on, it'll notify you.

@ahmadawais
Copy link

@trepmal is it work in progress? Coz it ain't working at my side.

@tonydew
Copy link

tonydew commented Jan 6, 2016

@ahmadawais see title: "WordPress experiment"

@pdaalder
Copy link

pdaalder commented Jan 7, 2016

Works like a charm! Nice work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment