WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Шаблоны сайтов на русском

get_editable_roles() WP 2.8.0

Возвращает отфильтрованный список ролей пользователей.

Для работы функции во фронте нужно подключить файл:

require_once ABSPATH . 'wp-admin/includes/user.php';

Изменить список можно с помощью фильтра editable_roles.

Является основой для: wp_dropdown_roles()
Работает на основе: wp_roles()
✈ 1 раз = 0.000358с = быстро | 50000 раз = 8.56с = быстро | PHP 7.0.32, WP 5.1
Хуки из функции
Возвращает

Массив. Массив массивов, содержащих информацию о роли (название + возможности).

Использование

get_editable_roles();

Примеры

#1 Что возвращает функция

Пример возвращаемых данных на чистой установке WordPress:

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
					[update_core] => 1
					[list_users] => 1
					[remove_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
				)

		)

	[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
				)

		)

)

[06-Mar-2019 16:01:56 UTC] 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
					[update_core] => 1
					[list_users] => 1
					[remove_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
				)

		)

	[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
				)

		)

)

#2 Выведем список ролей в выпадающем списке

Ниже код функции движка wp_dropdown_roles(), в основе которой используется описываемая функция.

function wp_dropdown_roles( $selected = '' ) {
	$r = '';

	$editable_roles = array_reverse( get_editable_roles() );

	foreach ( $editable_roles as $role => $details ) {
		$name = translate_user_role( $details['name'] );
		// preselect specified role
		if ( $selected == $role ) {
			$r .= "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>";
		} else {
			$r .= "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
		}
	}

	echo $r;
}

Код get editable roles: wp-admin/includes/user.php VER 5.1.1

<?php
function get_editable_roles() {
	$all_roles = wp_roles()->roles;

	/**
	 * Filters the list of editable roles.
	 *
	 * @since 2.8.0
	 *
	 * @param array[] $all_roles Array of arrays containing role information.
	 */
	$editable_roles = apply_filters( 'editable_roles', $all_roles );

	return $editable_roles;
}

Cвязанные функции

Из метки: Роли и возможности (role capabilities)

Еще из раздела: Остальное

campusboy 3279youtube.com/c/wpplus
Создатель YouTube канала wp-plus, на котором делюсь своим опытом. Активный пользователь wp-kama.ru. WordPress-разработчик. Разработка сайтов и лендингов. Доработка существующих проектов. Сопровождение ресурсов.
Редакторы: Kama 7364
Комментариев нет
    Здравствуйте, !     Войти . Зарегистрироваться