I Made a menu to remove admin menus for people that are not allowed to see it so they don't make changes to the site.
But I can't get submenus to display only the main menu's
this is the code:
<?php
class clean_Admin_Menu {
public $toggleItemSlug = 'toggle_cleanadminmenu';
public $toggleItemOrder = '300.1';
public $hiddenItemsOptionName = 'toggle_cleanadminmenu_items';
public $nonceName = 'toggle_cleanadminmenu_options';
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
//add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
//add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
//action to add menu pages on admin
add_action( 'admin_menu', array( $this, 'addMenuPages' ) );
//action for adding classes to admin menu items
add_action( 'admin_menu', array( $this, 'adminMenuAction' ) );
}
//Checks if users has the correct email and removes menu's if the user id doens't match.
public function toggle_menu_items() {
$current_user = wp_get_current_user();
//Getting emails from database(q_admin)
global $wpdb;
$table_name= $wpdb->prefix.'q_admin';
$DBP_results= $wpdb->get_results("SELECT * FROM $table_name");
$showMenu = false;
//Check if emails match with the 2 databases
foreach($DBP_results as $DBP_cols){
if ((string)$current_user->data->user_email == (string)$DBP_cols->email){
$showMenu = true;
break;
} else {
$showMenu = false;
}
}
if ( $showMenu ) {
// logged in.
?>
<script>
(function($) {
var menusAreHidden = false;
$(function() {
$('.menu-top.clean-wp-admin-menu__valid-item').show();
$(document).trigger('wp-window-resized');
//When the toggle extra item clicked show/hide menu items
//Also trigger the wp-window-resized event for left menu
$('#toplevel_page_toggle_cleanadminmenu a').click(function(e){
e.preventDefault();
$('.menu-top.clean-wp-admin-menu__valid-item').show();
$(document).trigger('wp-window-resized');
});
$('#adminmenu .wp-menu-open.hidden').removeClass('hidden');
});
})(jQuery);
</script>
<?php
} else {
// not Logged in.
}
}
//Redirect function when there are erros.
public function admin_init() {
if ( is_admin() ) {
ob_start();
add_action( 'admin_head', array( $this, 'toggle_menu_items' ) );
}
}
/*
// function to toggle the menu.
public function toggle_menu_items2() {
?>
<script>
(function($) {
var menusAreHidden = false;
$(function() {
$('.menu-top.clean-wp-admin-menu__valid-item').show();
$(document).trigger('wp-window-resized');
//When the toggle extra item clicked show/hide menu items
//Also trigger the wp-window-resized event for left menu
$('#toplevel_page_toggle_cleanadminmenu a').click(function(e){
e.preventDefault();
$('.menu-top.clean-wp-admin-menu__valid-item').show();
$(document).trigger('wp-window-resized');
});
$('#adminmenu .wp-menu-open.hidden').removeClass('hidden');
});
})(jQuery);
</script>
<?php
}
*/
//Add menu pages in admin
public function addMenuPages() {
//toggle menu button
/*
add_menu_page(
__('Toggle Menu', 'clean-admin-menu'),
__('Toggle Menu', 'clean-admin-menu'),
'manage_options',
$this->toggleItemSlug,
function () { return false; },
"dashicons-hidden",
$this->toggleItemOrder
);
*/
add_menu_page(
__('Q Admin', 'clean-admin-menu'),
__('Q Admin', 'clean-admin-menu'),
'manage_options',
'clean-admin-menu_options',
array(
$this,
'settingspage'
),
get_stylesheet_directory_uri() . '/assets/img/q-favicon.png',
);
//action for adding css
wp_enqueue_style( 'style.css', get_stylesheet_uri() );
add_submenu_page ( 'clean-admin-menu_options', 'Q Menu', 'Admin List', 'read', 'theme-options.php', 'call_back_q');
function call_back_q(){
echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
<h2></h2></div><br>';
include ('admin_list.php');
}
}
// Add items to display the menus correctly
public function adminMenuAction() {
global $_registered_pages, $_parent_pages, $menu, $admin_page_hooks, $submenu;
global $self, $parent_file, $submenu_file, $plugin_page, $typenow, $_wp_real_parent_file;
//list of items selected from settings page
$selectedItems = $this->selectedItems();
$menuItems = wp_list_pluck($menu, 2);
foreach ($menu as $k => $item) {
// Reminder for parent menu items array
// 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes, 5 = hookname, 6 = icon_url
$isSelected = in_array($item[2], $selectedItems);
$isCurrentItem = false;
$isCurrentParent = false;
//check if item is parent of current item
//if not both of them, it deserves to be hidden if it is selected
if ($parent_file) {
$isCurrentItem = ($item[2] == $parent_file);
if (isset($_parent_pages[$parent_file])) {
$isCurrentParent = ($_parent_pages[$parent_file] === $item[2]);
}
}
$isHidden = ($isSelected && false === ($isCurrentParent OR $isCurrentItem));
if ($isHidden) {
$menu[$k][4] = $item[4] . ' hidden clean-wp-admin-menu__valid-item';
}
}
}
public function settingsPage() {
global $_registered_pages, $_parent_pages, $menu, $admin_page_hooks, $submenu;
$this->saveSettings();
$pluginName = 'clean-admin-menu';
$selectedItems = $this->selectedItems();
?>
<style>
.wrap td, .wrap th { text-align: left; }
.table-menulist{ background-color: #fff; padding: 10px; margin-bottom: 20px; }
.table-menulist th { padding: 5px; border-bottom: 1px solid #DFDFDF; }
.table-menulist td { padding: 5px; border-bottom: 1px solid #DFDFDF; }
.table-menulist tr:last-child td { border-bottom: 0;}
.table-menulist .awaiting-mod, .table-menulist .update-plugins {display: inline-block;vertical-align: top;box-sizing: border-box;margin: 1px 0 -1px 2px;padding: 0 5px;border-radius: 9px;color: #fff;font-size: 11px;line-height: 1.6;text-align: center;z-index: 26;}
</style>
<div class="content">
<div id="main">
<div class="wrap">
<h1><?php esc_html_e('Q Admin Menu', $pluginName); ?></h1>
<?php
echo (($_GET['saved']==1)?'<div class="updated"><p>' . __('Success! Admin menu cleaned successfully', 'clean-admin-menu') . '</p></div>':'');
?>
<form action="<?php echo esc_attr(admin_url('admin.php?page=clean-admin-menu_options')); ?>" method="post">
<?php wp_nonce_field($this->nonceName, $this->nonceName, true, true); ?>
<input type="submit" class="button-primary" value="<?php esc_html_e('SAVE CHANGES', $pluginName); ?>"/>
<table class="table-menulist">
<tr>
<th></th>
<th></th>
<th style="width:300px;">Menu Items</th>
</tr>
<?php
$separator = 0;
foreach ($menu as $key => $menuItem){
$isSeparator = strpos($menuItem[4], 'wp-menu-separator');
$isSelected = in_array($menuItem[2], $selectedItems);
//if ($isSeparator !== false OR $menuItem[2] === 'toggle_cleanadminmenu') {
if ($isSeparator !== false) {
$menuItem[0] = '――――――separator――――――';
$separator++;
}
// Hiding the Separator before the "toggle menu" link
if($separator > 1) { $separator = 0; continue; }
?>
<tr>
<td>
<input type="checkbox" name="toggle_cleanadminmenu_items[]" value="<?php echo $menuItem[2]; ?>"
id="toggle_cleanadminmenu_item_<?php echo $key; ?>"
<?php echo ($isSelected) ? 'checked' : ''; ?>
<?php //echo ($menuItem[2] === 'index.php') ? 'disabled' : ''; ?> />
</td>
<td>
<?php if ($isSelected){ ?>
<span style="color:#CA4A1F;" class="dashicons-before dashicons-hidden"></span>
<?php }else{?>
<span style="color:#18dc0b;" class="dashicons-before dashicons-visibility"></span>
<?php } ?>
</td>
<td>
<label for="toggle_cleanadminmenu_item_<?php echo $key; ?>">
<strong <?php echo ($isSeparator !== false?'style="color:#B7B7B7;"':'')?>>
<?php
if ($menuItem[2] === 'clean-admin-menu_options')
echo '―― '.strtoupper($menuItem[0]).' ――<br><sub style="color:#616A74;">Alleen zichtbaar voor super admins</sub>';
else
echo $menuItem[0];
?>
</strong>
</label>
</td>
</tr>
<?php } ?>
</table>
</form>
<hr>
</div>
</div>
</div>
<?php
}
public function selectedItems() {
$items = get_option($this->hiddenItemsOptionName);
if (!$items) {
$items = array();
return $items;
}
return $items;
}
private function saveSettings() {
global $menu;
if (!isset($_POST[$this->nonceName])) {
return false;
}
$verify = check_admin_referer($this->nonceName, $this->nonceName);
//TODO if empty but has post delete items
if (!isset($_POST['toggle_cleanadminmenu_items'])) {
$itemsToSave = array();
$savedSuccess = 0;
} else {
$menuItems = wp_list_pluck($menu, 2);
$items = $_POST['toggle_cleanadminmenu_items'];
//save and check to see if it exists
$itemsToSave = array();
if ($items) {
foreach ($items as $item) {
if (in_array($item, $menuItems)) {
$itemsToSave[] = $item;
}
}
}
$savedSuccess = 1;
}
//update and set as autoloading
update_option($this->hiddenItemsOptionName, $itemsToSave, true);
// we'll redirect to same page when saved to see results.
$adminPageUrl = admin_url('options-general.php?page=clean-admin-menu_options&saved='.$savedSuccess);
wp_safe_redirect( $adminPageUrl ); exit;
}
}
new clean_Admin_Menu();
?>
I don't know how to get it to work hope somebody is able to help me.