WordPress Plugin to create custom role and capability ,grant access to only our custom admin menu

WordPress Plugin to create custom role and capability ,add capability to our role and grant access to only our custom admin menu

<?php
/**
 * @package Custom Role & Capability
 */
/*
Plugin Name: Custom Role & Capability
Plugin URI: http://unitedwebsoft.in/
Description: Create custom role and capability  , add capability to our role and grant access to only our custom admin menu
Version: 1.0.0
Author: unitedWebSoft
Author URI: http://unitedwebsoft.in/
 
Text Domain: custom_role_capability
*/

//Create custom role 'manager' on our plugin activation


register_activation_hook(__FILE__,'activation');
function activation(){
 	
	/********************************************************************/
	//add new role 'manager'
		add_role( 'manager', 'Manager', array(
		'read' => true, // True allows that capability
		'manager_access_only'=>true,
		'delete_posts' => false, // Use false to explicitly deny
	) );
 	
	
	}
	
	
//Delete our custom role 'manager' after plugin deactivated	
register_deactivation_hook(__FILE__,'dectivation');
function dectivation(){
	remove_role('manager');
}


//add new capability on admin init i.e 'manager_access_only' for role 'manager' and 'administrator'
add_action( 'admin_init', 'add_capability');
function add_capability() {
   //add new capability
	 $role = get_role( 'manager' );
	 $role->add_cap( 'manager_access_only' ); 
	 
	 $role2 = get_role( 'administrator' );
	 $role2->add_cap( 'manager_access_only' ); 	
}


// create custom menu and hide extra admin menu items for role 'manager'

add_action('admin_menu','register_wc_extend_menu'); 

function register_wc_extend_menu(){
		add_menu_page('Custom Admin Menu','Custom Admin Menu','manager_access_only','custom_menu','custom_menu'); 
		
		 	/**********************Hide some pages for a role 'manager'**********/
			if(current_user_can('manager')){
			    remove_menu_page( 'profile.php' );                   //Profile
			  remove_menu_page( 'edit.php' );                   //Posts
			  remove_menu_page( 'upload.php' );                 //Media
			  remove_menu_page( 'edit.php?post_type=page' );    //Pages
			  remove_menu_page( 'edit-comments.php' );          //Comments
			  remove_menu_page( 'themes.php' );                 //Appearance
			  remove_menu_page( 'plugins.php' );                //Plugins
			  remove_menu_page( 'users.php' );                  //Users
			  remove_menu_page( 'tools.php' );                  //Tools
			  remove_menu_page( 'options-general.php' );        //Settings
			}

}

function custom_menu(){
	echo "Here menu page contents";
}

?>

Visit us For advance level of PHP ,Drupal, WordPress, Magento training in Delhi/NCR .

Online Web Development Training,Video Tutorials