get_role ('administrator') returning null

0

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

$admin = get_role( 'administrator' );

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

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

I've tried several ways for a solution to this but I do not think so.

    
asked by anonymous 21.11.2016 / 17:01

1 answer

1

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

No 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 is a list of default capabilities for each user level.

    
23.11.2016 / 01:27