更新日期:2022-06-14

WordPress 有六個預設的角色,分別是:Super Admin, Administrator, Editor, Author, Contributor and Subscriber。而每一個角色,又包含一些能力組合,例如擁有 edit_posts 就可以編輯文章。底下會介紹幾個常用關於 role 的函數。

取得所有角色清單指令:

wp_roles()->get_names();

會得到角色的陣列大概像這樣:

array(
  [administrator] => Administrator,
  [editor] => Editor,
  [author] => Author,
  [contributor] => Contributor,
  [subscriber] => Subscriber
  ......
);

想要知道所有角色搭配哪些能力,可以用下方指令:

wp_roles()->roles;

若要知道單一角色 editor 有哪些能力,可以下:

wp_roles()->get_role('editor')