Роли пользователей в WordPress

WordPress разграничивает пользователей по ролям. Эти роли можно изменить программно: добавить, удалить роль. А так же существует повзможность отредактировать список ролей конкретного пользователя.

Получение возможности роли

Функция "get_role" возвращает все возможности указанной роли. Название роли (administrator, editor, author, contributor, subscriber) передаётся функции "get_role" в качестве единственного аргумента:
$role = get_role( 'author' );
print_r( $role );
В результате выполнения этого кода будет распечатана часть объекта "WP_Role", содержащего возможности только для роли автора ('author'):
WP_Role Object
(
   [name] => author
   [capabilities] => Array
      (
         [upload_files] => 1
         [edit_posts] => 1
         [edit_published_posts] => 1
         [publish_posts] => 1
         [read] => 1
         [level_2] => 1
         [level_1] => 1
         [level_0] => 1
         [delete_posts] => 1
         [delete_published_posts] => 1
      )
)

Добавление роли

Чтобы добавить новую роль в WordPress, воспользуйтесь функцией "add_role". Эта функция может принимать три параметра:
add_role( $role, $display_name, $capabilities );
Название Тип Описание
$role строка Символьный код роли. Возможные значения: administrator, editor, author, contributor, subscriber
$display_name строка Отображаемое название для роли после создания
$capabilities массив Массив возможностей новой роли
Функция "add_role" возвращает объект "WP_Role" в случае успешного добавлени новой роли или "null", если добавить роль не удалось. Основная причина неудачного выполнения функции - существование роли с таким символьным кодом.

Продемонстрируем процесс добавления новой роли. Пользователь с этой ролью сможет читать и изменять посты, а также загружать файлы:
$role = add_role( 'qrator', 'Куратор',
   array(
      'read' => true, 
      'edit_posts' => true, 
      'upload_files' => true, 
   )
);
if( $role !== null ) {
   echo 'Создана роль "Куратор"';
}else{
   echo 'Не удалось создать роль "Куратор"';
}

Удаление роли

Удалить роль можно с помощью функции "remove_role". У этой функции только один аргумент - символьный код роли (administrator, editor, author, contributor, subscriber или любое другое, что было создано самостоятельно):
remove_role( $role );
Эта функция ничего не возвращает в процессе своей работы.

Попробуем удалить роль "qrator", которая была создана ранее, с помощью функции "add_role".
remove_role( 'qrator' );

Получение всех ролей WordPress

В образовательных целях стоит хотя бы раз получить вывод функции "wp_roles". Эта функция возвращает весь объект "WP_Roles", который содержит описание ролей пользователей и хранится в глобальной переменно "$wp_roles". Попробуем это сделать:
$roles = wp_roles();
print_r( $roles );
По сути функция "get_role" выдаёт только часть того, что выдаёт "wp_roles". Приведённый выше код выведет на экран довольно большое описание всех ролей сайта:
WP_Roles Object
(
   [roles] => Array
      (
         [administrator] => Array
            (
               [name] => Administrator
               [capabilities] => Array
                  (
                     [switch_themes] => 1
                     [edit_themes] => 1
                     [activate_plugins] => 1
                     [edit_plugins] => 1
                     [edit_users] => 1
                     [edit_files] => 1
                     [manage_options] => 1
                     [moderate_comments] => 1
                     [manage_categories] => 1
                     [manage_links] => 1
                     [upload_files] => 1
                     [import] => 1
                     [unfiltered_html] => 1
                     [edit_posts] => 1
                     [edit_others_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [edit_pages] => 1
                     [read] => 1
                     [level_10] => 1
                     [level_9] => 1
                     [level_8] => 1
                     [level_7] => 1
                     [level_6] => 1
                     [level_5] => 1
                     [level_4] => 1
                     [level_3] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [edit_others_pages] => 1
                     [edit_published_pages] => 1
                     [publish_pages] => 1
                     [delete_pages] => 1
                     [delete_others_pages] => 1
                     [delete_published_pages] => 1
                     [delete_posts] => 1
                     [delete_others_posts] => 1
                     [delete_published_posts] => 1
                     [delete_private_posts] => 1
                     [edit_private_posts] => 1
                     [read_private_posts] => 1
                     [delete_private_pages] => 1
                     [edit_private_pages] => 1
                     [read_private_pages] => 1
                     [delete_users] => 1
                     [create_users] => 1
                     [unfiltered_upload] => 1
                     [edit_dashboard] => 1
                     [update_plugins] => 1
                     [delete_plugins] => 1
                     [install_plugins] => 1
                     [update_themes] => 1
                     [install_themes] => 1
                     [manage_downloads] => 1
                     [manage_database] => 1
                     [democracy_admin] => 1
                     [update_core] => 1
                     [list_users] => 1
                     [remove_users] => 1
                     [add_users] => 1
                     [promote_users] => 1
                     [edit_theme_options] => 1
                     [delete_themes] => 1
                     [export] => 1
                  )

            )

         [editor] => Array
            (
               [name] => Editor
               [capabilities] => Array
                  (
                     [moderate_comments] => 1
                     [manage_categories] => 1
                     [manage_links] => 1
                     [upload_files] => 1
                     [unfiltered_html] => 1
                     [edit_posts] => 1
                     [edit_others_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [edit_pages] => 1
                     [read] => 1
                     [level_7] => 1
                     [level_6] => 1
                     [level_5] => 1
                     [level_4] => 1
                     [level_3] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [edit_others_pages] => 1
                     [edit_published_pages] => 1
                     [publish_pages] => 1
                     [delete_pages] => 1
                     [delete_others_pages] => 1
                     [delete_published_pages] => 1
                     [delete_posts] => 1
                     [delete_others_posts] => 1
                     [delete_published_posts] => 1
                     [delete_private_posts] => 1
                     [edit_private_posts] => 1
                     [read_private_posts] => 1
                     [delete_private_pages] => 1
                     [edit_private_pages] => 1
                     [read_private_pages] => 1
                     [democracy_admin] => 1
                  )

            )

         [author] => Array
            (
               [name] => Author
               [capabilities] => Array
                  (
                     [upload_files] => 1
                     [edit_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [read] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [delete_posts] => 1
                     [delete_published_posts] => 1
                  )

            )

         [contributor] => Array
            (
               [name] => Contributor
               [capabilities] => Array
                  (
                     [edit_posts] => 1
                     [read] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [delete_posts] => 1
                  )

            )

         [subscriber] => Array
            (
               [name] => Subscriber
               [capabilities] => Array
                  (
                     [read] => 1
                     [level_0] => 1
                  )

            )

      )

   [role_objects] => Array
      (
         [administrator] => WP_Role Object
            (
               [name] => administrator
               [capabilities] => Array
                  (
                     [switch_themes] => 1
                     [edit_themes] => 1
                     [activate_plugins] => 1
                     [edit_plugins] => 1
                     [edit_users] => 1
                     [edit_files] => 1
                     [manage_options] => 1
                     [moderate_comments] => 1
                     [manage_categories] => 1
                     [manage_links] => 1
                     [upload_files] => 1
                     [import] => 1
                     [unfiltered_html] => 1
                     [edit_posts] => 1
                     [edit_others_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [edit_pages] => 1
                     [read] => 1
                     [level_10] => 1
                     [level_9] => 1
                     [level_8] => 1
                     [level_7] => 1
                     [level_6] => 1
                     [level_5] => 1
                     [level_4] => 1
                     [level_3] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [edit_others_pages] => 1
                     [edit_published_pages] => 1
                     [publish_pages] => 1
                     [delete_pages] => 1
                     [delete_others_pages] => 1
                     [delete_published_pages] => 1
                     [delete_posts] => 1
                     [delete_others_posts] => 1
                     [delete_published_posts] => 1
                     [delete_private_posts] => 1
                     [edit_private_posts] => 1
                     [read_private_posts] => 1
                     [delete_private_pages] => 1
                     [edit_private_pages] => 1
                     [read_private_pages] => 1
                     [delete_users] => 1
                     [create_users] => 1
                     [unfiltered_upload] => 1
                     [edit_dashboard] => 1
                     [update_plugins] => 1
                     [delete_plugins] => 1
                     [install_plugins] => 1
                     [update_themes] => 1
                     [install_themes] => 1
                     [manage_downloads] => 1
                     [manage_database] => 1
                     [democracy_admin] => 1
                     [update_core] => 1
                     [list_users] => 1
                     [remove_users] => 1
                     [add_users] => 1
                     [promote_users] => 1
                     [edit_theme_options] => 1
                     [delete_themes] => 1
                     [export] => 1
                  )

            )

         [editor] => WP_Role Object
            (
               [name] => editor
               [capabilities] => Array
                  (
                     [moderate_comments] => 1
                     [manage_categories] => 1
                     [manage_links] => 1
                     [upload_files] => 1
                     [unfiltered_html] => 1
                     [edit_posts] => 1
                     [edit_others_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [edit_pages] => 1
                     [read] => 1
                     [level_7] => 1
                     [level_6] => 1
                     [level_5] => 1
                     [level_4] => 1
                     [level_3] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [edit_others_pages] => 1
                     [edit_published_pages] => 1
                     [publish_pages] => 1
                     [delete_pages] => 1
                     [delete_others_pages] => 1
                     [delete_published_pages] => 1
                     [delete_posts] => 1
                     [delete_others_posts] => 1
                     [delete_published_posts] => 1
                     [delete_private_posts] => 1
                     [edit_private_posts] => 1
                     [read_private_posts] => 1
                     [delete_private_pages] => 1
                     [edit_private_pages] => 1
                     [read_private_pages] => 1
                     [democracy_admin] => 1
                  )

            )

         [author] => WP_Role Object
            (
               [name] => author
               [capabilities] => Array
                  (
                     [upload_files] => 1
                     [edit_posts] => 1
                     [edit_published_posts] => 1
                     [publish_posts] => 1
                     [read] => 1
                     [level_2] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [delete_posts] => 1
                     [delete_published_posts] => 1
                  )

            )

         [contributor] => WP_Role Object
            (
               [name] => contributor
               [capabilities] => Array
                  (
                     [edit_posts] => 1
                     [read] => 1
                     [level_1] => 1
                     [level_0] => 1
                     [delete_posts] => 1
                  )

            )

         [subscriber] => WP_Role Object
            (
               [name] => subscriber
               [capabilities] => Array
                  (
                     [read] => 1
                     [level_0] => 1
                  )

            )

      )

   [role_names] => Array
      (
         [administrator] => Administrator
         [editor] => Editor
         [author] => Author
         [contributor] => Contributor
         [subscriber] => Subscriber
      )

   [role_key] => wp_user_roles
   [use_db] => 1
)
MouseDC.ru - хостинг, виртуальный хостинг, покупка доменов, проверка доменов, WHOIS, курсы создания сайтов, вебинары по созданию, курсы разработки сайтов, доработка сайтов, сопровождение сайтов, разработка сайтов, техподдержка сайтов
Cмотрите другие статьи:
Была ли статья полезной?
Была ли эта статья полезна? Есть вопрос?
хостинг для сайтов
Закажите недорогой хостинг Заказать

всего от 290 руб

⇡ наверх