Skip to content

Instantly share code, notes, and snippets.

@JRyven
Last active October 26, 2021 08:45
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 JRyven/d7a5d1ecee1367e0d81469c882ef086f to your computer and use it in GitHub Desktop.
Save JRyven/d7a5d1ecee1367e0d81469c882ef086f to your computer and use it in GitHub Desktop.
Add a simple button to the WordPress administrator toolbar that will remove the admin toolbar from the DOM.
<?php
/* Click_Hide_Admin_Toolbar
*
*
*/
class Click_Hide_Admin_Toolbar {
public function __construct(){
$this->scripts();
$this->styles();
add_action( 'admin_bar_menu', [ $this, 'chat_button' ], 999 );
}
public function chat_button( $admin_bar ){
$admin_bar->add_menu( array(
'id' => 'chat-button',
'title' => 'Hide',
'href' => '#',
'meta' => array(
'title' => __('Click to hide the admin toolbar. Toolbar will reappear on refresh.'),
'onclick' => __('click_hide_admin_toolbar_nth_patent(this, 4).remove();'),
),
));
}
public function scripts() {
ob_start(); ?>
function click_hide_admin_toolbar_nth_patent(element, n) {
while( n-- && element )
element = element.parentNode;
return element;
}
<?php $chat_scripts = ob_get_clean();
if ( ! wp_script_is( 'chat_scripts', 'enqueued' ) ) {
wp_register_script( 'chat_scripts', $script_uri, array(), '1.0.0' );
wp_enqueue_script( 'chat_scripts' );
}
wp_add_inline_script( 'chat_scripts', $chat_scripts );
}
public function styles() {
ob_start(); ?>
li#wp-admin-bar-chat-button a {
border-width: 0px 1px 0px 1px;
border-color: #f0f0f180;
border-style: solid;
}
li#wp-admin-bar-chat-button a:hover {
border-width: 0px 1px 0px 1px;
border-color: #FFFFFF;
border-style: solid;
color: #FFFFFF;
}
<?php $chat_styles = ob_get_clean();
if ( ! wp_style_is( 'chat_styles', 'enqueued' ) ) {
wp_register_style( 'chat_styles', FALSE );
wp_enqueue_style( 'chat_styles' );
}
wp_add_inline_style( 'chat_styles', $chat_styles );
}
} // close the Class
/* Call the Class
*
*
*/
add_action('wp_head', 'chat_activate');
function chat_activate(){
new Click_Hide_Admin_Toolbar();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment