出于某种原因,我无法使用WordPress函数get_editable_roles()。这有错误吗? 不介意我在functions.php中添加的位置,在index.php中,在顶部,在底部。它总是给出未定义的函数错误。
我在Twenty Fifteen主题中添加了这行代码,在一个全新的WordPress安装中,没有任何插件:
$role = get_editable_roles();
它给出了这个错误:
错误:调用未定义的函数get_editable_roles()...
如果我之前加载了user.php文件,那么它可以工作:
if (!function_exists('get_editable_roles')) {
require_once(ABSPATH . '/wp-admin/includes/user.php');
}
答案 0 :(得分:1)
我认为问题在于您尝试在未加载的管理部分之外使用此功能。
从Wordpress文档:
备注强>
定义此函数的文件(wp-admin / includes / user.php)仅在管理部分中加载。
请参阅:https://codex.wordpress.org/Function_Reference/get_editable_roles
答案 1 :(得分:0)
即使您在管理员方面,也应该考虑加载 user.php ,尤其是您已经创建了一个类似于选项的自定义管理页面。
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$roles = get_editable_roles();
还讨论了here。