Created
September 16, 2024 13:17
-
-
Save bhowe/e37398f067d736b7f71fc7d50acb975f to your computer and use it in GitHub Desktop.
Control what a client can see in WordPre
This file contains hidden or 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
// Create the client_admin role | |
function create_client_admin_role() { | |
add_role( | |
'client_admin', | |
'Client Admin', | |
get_role('administrator')->capabilities | |
); | |
} | |
add_action('init', 'create_client_admin_role'); | |
// Modify admin menu for client_admin | |
function modify_admin_menu_for_client_admin() { | |
if (current_user_can('client_admin')) { | |
global $menu, $submenu; | |
// Get all post types | |
$args = array( | |
'public' => true, | |
'_builtin' => false | |
); | |
$custom_post_types = get_post_types($args, 'names', 'and'); | |
// Define menu items to keep | |
$keep_menu_items = array( | |
'edit.php', // Posts | |
'edit.php?post_type=page', // Pages | |
'upload.php', // Media | |
'nav-menus.php', // Menus | |
'themes.php' // Required for accessing Menus | |
); | |
// Add custom post types to the keep list | |
foreach ($custom_post_types as $post_type) { | |
$keep_menu_items[] = "edit.php?post_type={$post_type}"; | |
} | |
// Remove menu items | |
foreach ($menu as $key => $item) { | |
if (!in_array($item[2], $keep_menu_items)) { | |
remove_menu_page($item[2]); | |
} | |
} | |
// Remove specific submenu items that we don't want to keep | |
$remove_submenu_items = array( | |
'upload.php' => array('media-new.php'), // Remove "Add New" under Media | |
'themes.php' => array('themes.php', 'theme-editor.php', 'customize.php') // Remove theme-related items | |
); | |
foreach ($remove_submenu_items as $parent => $items) { | |
foreach ($items as $item) { | |
remove_submenu_page($parent, $item); | |
} | |
} | |
// Ensure Menus is accessible under Appearance | |
if (isset($submenu['themes.php'])) { | |
foreach ($submenu['themes.php'] as $key => $item) { | |
if ($item[2] === 'nav-menus.php') { | |
continue; // Keep Menus | |
} | |
remove_submenu_page('themes.php', $item[2]); | |
} | |
} | |
} | |
} | |
add_action('admin_menu', 'modify_admin_menu_for_client_admin', 999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment