Skip to content

Instantly share code, notes, and snippets.

@flyingwebie
Created July 8, 2024 14:12
Show Gist options
  • Save flyingwebie/c00632f1c485173489fa1bf82daf4337 to your computer and use it in GitHub Desktop.
Save flyingwebie/c00632f1c485173489fa1bf82daf4337 to your computer and use it in GitHub Desktop.
This WordPress script allows specific users to toggle the visibility of selected admin menu items. It adds a toggle button to the admin bar, enabling easy switching between full and restricted admin views.
<?php
// Configuration - Users who can see everything and use the toggle
$GLOBALS['usernames_to_not_hide'] = array('superadmin');
// Multiusers
//$GLOBALS['usernames_to_not_hide'] = array('superuser1', 'superuser2');
// Update the pages_to_hide array
$GLOBALS['pages_to_hide'] = array(
'index.php' => array('update-core.php'),
'themes.php',
'plugins.php',
'tools.php',
'edit.php',
'users.php',
'options-general.php',
'automatic-css',
'rank-math',
'meta-box',
'wp-mail-smtp',
'mepr-toolbox',
'bricks',
);
// Add toggle button to admin bar for specified users
add_action('admin_bar_menu', 'add_toggle_button', 999);
function add_toggle_button($admin_bar) {
$usernames_to_not_hide = isset($GLOBALS['usernames_to_not_hide']) ? $GLOBALS['usernames_to_not_hide'] : array();
$current_user = wp_get_current_user();
if (!in_array($current_user->user_login, $usernames_to_not_hide)) {
return; // Don't add the toggle for users not in the list
}
$is_active = get_option('hide_admin_elements_active', false);
$button_text = $is_active ? 'Disable Admin Hide' : 'Enable Admin Hide';
$button_class = $is_active ? 'hide-admin-active' : 'hide-admin-inactive';
$admin_bar->add_menu(array(
'id' => 'hide-admin-toggle',
'title' => '<span class="ab-icon"></span>' . $button_text,
'href' => '#',
'meta' => array(
'class' => $button_class,
'onclick' => 'return false;'
),
));
}
// Add JavaScript to handle toggle button click and page refresh
add_action('admin_footer', 'toggle_button_script');
function toggle_button_script() {
$usernames_to_not_hide = isset($GLOBALS['usernames_to_not_hide']) ? $GLOBALS['usernames_to_not_hide'] : array();
$current_user = wp_get_current_user();
if (!in_array($current_user->user_login, $usernames_to_not_hide)) {
return; // Don't add the script for users not in the list
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#wp-admin-bar-hide-admin-toggle').on('click', function(e) {
e.preventDefault();
const $button = $(this);
const $buttonText = $button.find('.ab-item').contents().filter(function() {
return this.nodeType === 3;
});
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'toggle_hide_admin_elements'
},
success: function(response) {
if(response.success) {
if(response.data.is_active) {
$button.addClass('hide-admin-active').removeClass('hide-admin-inactive');
$buttonText.replaceWith('Disable Admin Hide');
} else {
$button.addClass('hide-admin-inactive').removeClass('hide-admin-active');
$buttonText.replaceWith('Enable Admin Hide');
}
// Refresh the page after a short delay
setTimeout(function() {
location.reload();
}, 300);
}
}
});
});
});
</script>
<style>
#wp-admin-bar-hide-admin-toggle .ab-icon:before {
content: "\f160";
top: 2px;
}
#wp-admin-bar-hide-admin-toggle.hide-admin-active .ab-icon:before {
color: #dc3232;
}
#wp-admin-bar-hide-admin-toggle.hide-admin-inactive .ab-icon:before {
color: #46b450;
}
</style>
<?php
}
// AJAX handler for toggle button
add_action('wp_ajax_toggle_hide_admin_elements', 'toggle_hide_admin_elements');
function toggle_hide_admin_elements() {
$usernames_to_not_hide = isset($GLOBALS['usernames_to_not_hide']) ? $GLOBALS['usernames_to_not_hide'] : array();
$current_user = wp_get_current_user();
if (!in_array($current_user->user_login, $usernames_to_not_hide)) {
wp_send_json_error('Insufficient permissions');
}
$current_state = get_option('hide_admin_elements_active', false);
$new_state = !$current_state;
update_option('hide_admin_elements_active', $new_state);
wp_send_json_success(['is_active' => $new_state]);
}
// Function to hide admin elements on page load
add_action('admin_menu', 'hide_admin_elements', 999);
function hide_admin_elements() {
$usernames_to_not_hide = isset($GLOBALS['usernames_to_not_hide']) ? $GLOBALS['usernames_to_not_hide'] : array();
$pages_to_hide = isset($GLOBALS['pages_to_hide']) ? $GLOBALS['pages_to_hide'] : array();
$current_user = wp_get_current_user();
$is_active = get_option('hide_admin_elements_active', false);
if (!in_array($current_user->user_login, $usernames_to_not_hide) || $is_active) {
foreach ($pages_to_hide as $page => $subpages) {
if (is_string($subpages)) {
remove_menu_page($subpages);
} elseif (is_array($subpages)) {
foreach ($subpages as $parent => $children) {
if (is_array($children)) {
foreach ($children as $child) {
remove_submenu_page($parent, $child);
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment