Advertisement
hmbashar

Create Custom User Role

Aug 6th, 2015
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2.  
  3. /****************************************Computer Role ********************************/
  4.  
  5. /**
  6.  * We declare a constant here just to make it portable. Just edit the constant value to make changes.
  7.  */
  8. if( !defined( 'COMPUTER_ID' ) ) {
  9.     define( 'COMPUTER_ID', 'student', 'teachers' );
  10. }
  11.  
  12. /**
  13.  * First we create a new user role
  14.  */
  15.  
  16. add_action( 'init', 'add_computer_operator_role' );
  17.  
  18. if( !function_exists( 'add_computer_operator_role' ) ) {
  19.     function add_computer_operator_role() {
  20.  
  21.         // The default capabilities for our role. Modify it as per needed
  22.         $default_capabilities = array(
  23.             'read' => true,
  24.             'edit_posts' => true,
  25.             'delete_posts' => true,
  26.             'publish_posts' => true,
  27.             'upload_files' => true,
  28.             'manage_categories' => true,
  29.             'list_users' => true,
  30.             'create_users' => true,
  31.         );
  32.  
  33.         add_role( COMPUTER_ID . '_manager',
  34.             __( 'Computer Operator', 'edu_text_domain' ),
  35.             $default_capabilities
  36.         );
  37.     }
  38. }
  39.  
  40.  
  41. /**
  42.  * Now we should add our custom capabilities to the user role
  43.  */
  44. add_action( 'admin_init', 'add_computer_operator_role_caps' );
  45.  
  46. if( !function_exists( 'add_computer_operator_role_caps' ) ) {
  47.     function add_computer_operator_role_caps() {
  48.  
  49.         // By default, we add the capabilities to the custom role and administrator. You can allow more roles like editors or more custom roles to do these actions. Just simply include them into this array.
  50.         $roles = array( COMPUTER_ID . '_manager', 'administrator' );
  51.  
  52.         foreach( $roles as $role ) {
  53.  
  54.             $role = get_role( $role );
  55.  
  56.             $role->add_cap( 'read' );
  57.             $role->add_cap( 'read_' . COMPUTER_ID );
  58.             $role->add_cap( 'read_private_' . COMPUTER_ID );
  59.             $role->add_cap( 'edit_' . COMPUTER_ID );
  60.             $role->add_cap( 'edit_' .COMPUTER_ID . 's' );
  61.             $role->add_cap( 'edit_others_' . COMPUTER_ID . 's' );
  62.             $role->add_cap( 'edit_published_' . COMPUTER_ID . 's' );
  63.             $role->add_cap( 'publish_' . COMPUTER_ID . 's' );
  64.             $role->add_cap( 'delete_others_' . COMPUTER_ID . 's' );
  65.             $role->add_cap( 'delete_private_' . COMPUTER_ID . 's' );
  66.             $role->add_cap( 'delete_published_' . COMPUTER_ID . 's' );
  67.            
  68.            
  69.  
  70.             $role->add_cap( 'read_' . page );
  71.             $role->add_cap( 'read_private_' . page );
  72.             $role->add_cap( 'edit_' . page );
  73.             $role->add_cap( 'edit_' .page . 's' );
  74.             $role->add_cap( 'edit_others_' . page . 's' );
  75.             $role->add_cap( 'edit_published_' . page . 's' );
  76.             $role->add_cap( 'publish_' . page . 's' );
  77.             $role->add_cap( 'delete_others_' . page . 's' );
  78.             $role->add_cap( 'delete_private_' . page . 's' );
  79.             $role->add_cap( 'delete_published_' . page . 's' );
  80.            
  81.            
  82.            
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement