Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active September 4, 2020 15:42
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save trepmal/fa1ee9883c6d699bf5be to your computer and use it in GitHub Desktop.
Save trepmal/fa1ee9883c6d699bf5be 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
@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