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?
- 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 ) ); ?>
- 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 } ?>
- 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 } ?>
- 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:
Etiket: wordpress üye özel rol ekleme wp custom user role
Kat: Labs, WP Tricks Tarih: 4 Ağustos 2020 Okunma:165
Paylaş:
Kat: Labs, WP Tricks Tarih: 4 Ağustos 2020 Okunma:165
Paylaş:
Henüz yorum eklenmemiş.