Last active
September 4, 2020 15:42
-
-
Save trepmal/fa1ee9883c6d699bf5be to your computer and use it in GitHub Desktop.
WordPress experiment. Toggle debug from admin bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
@trepmal is it work in progress? Coz it ain't working at my side.
@ahmadawais see title: "WordPress experiment"
Works like a charm! Nice work :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.