WordPress Tutorial: Lock Themes and Restrict Access

If you run a blog with multiple administrators, there are probably times where you will want to restrict access to theme selection. This is help to prevent unwitting administrators from accidentally changing themes - trust me, an editor has done this to a site I was managing serving 10k+ impressions a day! Embarrassing...

To lock themes to only the user with the ID of 1 use the following code.

add_action( 'admin_init', 'lock_themes' );
function lock_themes()
{
    global $submenu, $userdata;
    if ( $userdata->ID != 1 ) {
        unset( $submenu['themes.php'][5] );
        unset( $submenu['themes.php'][15] );

        if ($_GET['access_error']) {
            add_action('admin_notices', 'page_access_admin_notice');
        }

        $restricted_access = array(
            '/wp-admin/themes.php'
        );

        if (in_array($_SERVER['REQUEST_URI'], $restricted_access)) {
            wp_redirect(get_option('siteurl') . '/wp-admin/index.php?access_error=true');
            exit;
        }

    }
}

function page_access_admin_notice()
{
    echo '<div id="permissions-warning" class="error fade"><p><strong>'.__("Unfortunately you don't have permission to access this page.").'</strong></p></div>';
}

Now when anyone tries to access the themes page they will be redirected to the WordPress admin index page and shown a friendly warning message. You can easily adjust the code to restrict access to other menu's and pages.