Using WordPress ‘populate_roles()’ PHP function

The populate_roles() WordPress PHP function executes role creation for various WordPress versions.

Usage

populate_roles();

Parameters

  • None

More information

See WordPress Developer Resources: populate_roles()

Examples

Creating default roles after a custom role removal

// Remove custom role 'manager'
remove_role('manager');

// Recreate default roles
populate_roles();

Refreshing roles after modifying capabilities

// Get 'editor' role and add a custom capability
$editor = get_role('editor');
$editor->add_cap('manage_categories');

// Refresh roles
populate_roles();

Resetting roles after importing users with custom roles

// Import users with custom roles

// Reset roles to default
populate_roles();

Fixing roles after a plugin or theme modification

// Update or modify plugin/theme

// Refresh default roles
populate_roles();

Reverting roles after temporarily adding a custom role

// Add temporary 'marketing' role
add_role('marketing', 'Marketing', array('read' => true));

// Perform tasks with the new role

// Revert back to default roles
populate_roles();