Skip to content

Instantly share code, notes, and snippets.

@bhowe
Created September 16, 2024 13:17

Revisions

  1. bhowe created this gist Sep 16, 2024.
    67 changes: 67 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    // 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);