Labs, WP Tricks

WP Custom User Role & If Else

Hello, Default Roles: Administrator: someone who has access to all the administrative features and functions within a site. Editor: someone who can publish and manage posts of all users, including […]

yzr: Hakkı Cengiz
A+ A A- okuma

Hello,

Default Roles:

  • Administrator: someone who has access to all the administrative features and functions within a site.
  • Editor: someone who can publish and manage posts of all users, including their own.
  • Author: someone who can publish and manage their own posts.
  • Contributor: someone who can write and manage their own posts but can’t publish them.
  • Subscriber: someone who can only manage their profile.

How to Add Custom User Role?

 

  1. functions.php:
    
    <?php 
    // Add a custom user role
        $result = add_role( 'caylak', __(
            'Çaylak' ),
            array(
            'read' => true,                 // true allows this capability
            'edit_posts' => true,           // Allows user to edit their own posts
            'edit_pages' => true,           // Allows user to edit pages
            'edit_others_posts' => true,    // Allows user to edit others posts not just their own
            'create_posts' => true,         // Allows user to create new posts
            'manage_categories' => true,    // Allows user to manage post categories
            'publish_posts' => true,        // Allows the user to publish, otherwise posts stays in draft mode
            'edit_themes' => false,         // false denies this capability. User can’t edit your theme
            'install_plugins' => false,     // User cant add new plugins
            'update_plugin' => false,       // User can’t update any plugins
            'update_core' => false          // user cant perform core updates
            )
        );
    ?>
        
  2. somewhere.php: // How to check if a user is in a specific role?
    
    <?php 
       $user = wp_get_current_user();
       if ( in_array( 'author', (array) $user->roles ) ) {
           //The user has the "author" role
       }
    ?>
        
  3. somewhere.php: // If you want to check more than two roles, you can check if the roles of the current user is inside an array of roles, something like:
    
    <?php 
      $user = wp_get_current_user();
      $allowed_roles = array( 'editor', 'administrator', 'author' );
      if ( array_intersect( $allowed_roles, $user->roles ) ) {
         // Stuff here for allowed roles
      }
    ?>
        
  4. Bonus: Security. Hide WP Dashboard Menu Items if user is not administrator.
    
    <?php 
        // Paneldeki Menuleri Gizle
        function my_remove_menu_items()
        {
            if (!current_user_can('administrator')):
                remove_menu_page('edit.php?post_type=cptslug');
                remove_menu_page('wpcf7');
                remove_menu_page('upload.php');
            endif;
        }
        add_action('admin_menu', 'my_remove_menu_items');
    ?>
        

 

 

Thanks:

0 Yorumları: - Yorum Formunu: -

Henüz yorum eklenmemiş.

Bi yoruma ne dersin ?

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir