Home Backend Development PHP Tutorial WordPress中创建用户角色的相关PHP函数使用详解_php技巧

WordPress中创建用户角色的相关PHP函数使用详解_php技巧

May 16, 2016 pm 08:01 PM
php wordpress Permissions user

WordPress 默认有 “订阅者”、“投稿者”、“作者”、“编辑” 和 “管理员” 五个用户角色,权限由低到高,但默认的五个角色可能不够我们用,这时可以使用 add_role() 函数创建一个角色。

用法

add_role( $role, $display_name, $capabilities );
Copy after login

参数

$role

(字符串)(必须)用户角色 ID

默认值:None

$display_name

(字符串)(必须)用户角色显示在前台的名字

默认值:None

$capabilities

(数组)(可选)用户角色的权限

默认值:None

返回值

如果此角色 ID 的角色不存在并且创建成功,则返回 WP_Role 类;用户角色已经存在或者创建失败则返回空。

例子

$result = add_role( 'new_role_created', __( '新建用户角色例子', 'Bing' ), array(
  'read' => true,
  'edit_posts' => true,
  'delete_posts' => false
) );
if( $result ) echo '用户角色创建成功!';
else echo '因为用户角色已经存在或者其它原因导致创建失败!';
Copy after login

其它

此函数位于:wp-includes/capabilities.php

WordPress 的用户系统看起来虽然很简单,但扩展性非常强,好好的利用插件,可以把 WordPress 的用户系统打造的不比专业的论坛系统差。

用户的权限管理是用户系统很重要的一部分,虽然这几个身份的权限设计的非常科学,但还是无法满足每个人的需求,本文就来教你怎么自定义这几个身份的权限。
首先安装并启用 Capability Manager Enhanced 插件,进入 “用户” → “Capabilities” 进行权限管理。

首先在右边的 “Select New Role” 选择一个要编辑权限的身份,然后在左边进行权限调整。

20151225152601421.png (1141×573)

调整好之后保存即可。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles