Home / Admin / Secure Admin Menu
Duplicate Snippet

Embed Snippet on Your Site

Secure Admin Menu

Use with caution. Remove unnecessary admin menus and redirect direct access to those pages.

Code Preview
php
<?php
// Secure Admin Menu / Kiri Vadivelu / 2026
add_action( 'admin_init', 'secure_admin_menu' );
function secure_admin_menu() {
    // Replace 1 with the actual User ID of the owner
    $owner_id = 1; 
    global $user_ID;
    // 1. If not owner, remove from sidebar
    if ( $user_ID != $owner_id ) {
        // remove_menu_page( 'edit.php' );            // Posts
        // remove_menu_page( 'upload.php' );          // Media
        // remove_menu_page( 'edit-comments.php' );   // Comments  
        // remove_menu_page( 'users.php' );           // Users
        remove_menu_page( 'plugins.php' );            // Plugins
        remove_menu_page( 'themes.php' );             // Appearance
        remove_menu_page( 'tools.php' );              // Tools
        remove_menu_page( 'options-general.php' );    // Settings
    }
    // 2. If not owner, redirect direct access
        $secured_pages = array('plugins.php','themes.php','tools.php', 'options-general.php' );
        $current_page = basename( $_SERVER['REQUEST_URI'] );
        
        if ( in_array( $current_page, $secured_pages ) ) {
            wp_redirect( admin_url() );
            exit;
        }
}  
  

Comments

Add a Comment