ThemesCreator
ThemesCreator

Reputation: 1759

Call to undefined function get_editable_roles()

For some reason, I can't get working the WordPress function get_editable_roles(). Is there a bug with this? Doesn't mind where I add, in the functions.php, in the index.php, in the top, in the bottom. It always gives the undefined function error.

I added this line of code in Twenty Fifteen theme, in a fresh WordPress install and without any plugin:

$role = get_editable_roles();

It give this error:

Error: Call to undefined function get_editable_roles()...

If I load the user.php file before, then it works:

if (!function_exists('get_editable_roles')) {
   require_once(ABSPATH . '/wp-admin/includes/user.php');
}

Upvotes: 2

Views: 3271

Answers (2)

kuzey beytar
kuzey beytar

Reputation: 3226

You should consider to load user.php even you are in admin side, especially you have created a custom admin page like options.

if ( ! function_exists( 'get_editable_roles' ) ) {
    require_once ABSPATH . 'wp-admin/includes/user.php';
}

$roles = get_editable_roles();

It is also discussed here.

Upvotes: 7

vicente
vicente

Reputation: 2633

I think the problem is that you're trying to use this function outside the admin section where it isn't loaded.

From the Wordpress documentation:

Notes
The file that defines this function (wp-admin/includes/user.php) is only loaded in the admin sections.

See: https://codex.wordpress.org/Function_Reference/get_editable_roles

Upvotes: 3

Related Questions