Restrict wp-admin access to certain users

Adding this snippet to the functions.php of your wordpress theme will restrict wp-admin access to certain users as defined at

http://codex.wordpress.org/Roles_and_Capabilities

And also still allows for user access to admin-ajax.php , async-upload.php .


function restrict_access_admin_panel(){
                global $current_user;
                get_currentuserinfo();
	
	if (
			// Look for the presence of /wp-admin/ in the url
			stripos($_SERVER['REQUEST_URI'],'/wp-admin/') !== false
			&&
			// Allow calls to async-upload.php
			stripos($_SERVER['REQUEST_URI'],'async-upload.php') == false
			&&
			// Allow calls to admin-ajax.php
			stripos($_SERVER['REQUEST_URI'],'admin-ajax.php') == false
		) {
	
				// Does the current user fail the required capability level?
                if (!current_user_can('activate_plugins')) {
                        wp_redirect( get_bloginfo('url') );
                        exit;
                }
			}		
        }
        add_action('admin_init', 'restrict_access_admin_panel', 1);