get_role('Administrator') returning null

Asked

Viewed 183 times

0

In a project using wordpress has a plugin that is giving error in a function call get_role.

$admin = get_role( 'administrator' );

This returns null and then in $admin->add_cap('regra') of a fatal error.

It’s as if wordpress has lost the admin rule.

I’ve looked in many ways for a solution to this but I don’t think.

  • Does the site have active users with this role? If it has it can only be a matter of recreating in the bank.

  • He does have permission. How could I recreate this in the bank?

1 answer

1


If the site has active users as administrators but the scroll has been edited in the database or removed, something like this may solve or at least help to understand the problem.

In the functions.php:

add_action( 'init', function() {
    // Imprime os roles existentes, pra conferir os role que já existem
    var_dump( wp_roles() );

    // Imprime o objeto WP_Role se foi criado, null se já existia
    var_dump( add_role( 'administrator', 'Administrator' ) );
} );

From here you can recreate the capabilities:

// Reseta as capabilities básicas do WP
// CUIDADO: se o seu código tiver retirado capabilities de algum 
// role elas podem retornar se você rodar assim.
populate_roles();

// Se precisar adicionar capabilities de forma mais granular, 
// use esse formato:
$role = get_role( 'administrator' );
$role->add_cap( 'activate_plugins' ); // repetir com todas as caps desejadas

You only need to run this once. Check the results on the screen, then you can take out the functions again.

Here’s a list of standard capabilities for each user level.

  • Actually the Administrator role did not appear in the first var dump and then started to appear. However when logging in, I am redirected to the site and not to the dashboard. Then trying to access /wp-admin appear the message "No permission to access this page." Since it is a new role, I would have to give all the usage capabilities for it with the add_cap function() ?

  • I updated the response with the code to recreate the capabilities. In a quick read of the core I thought they would be recreated automatically but it was a mistake, populate_roles() only wheel in upgrades.

  • I did using the second option using add_cap with the whole caps list for the administrator. The use of the function populate_roles() gave an error stating that the function does not exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.