Using WordPress ‘populate_roles_300()’ PHP function

The populate_roles_300() WordPress PHP function creates and modifies WordPress roles for WordPress 3.0.

Usage

populate_roles_300();

Parameters

  • None

More information

See WordPress Developer Resources: populate_roles_300

Examples

Creating and modifying WordPress roles

The following example uses the populate_roles_300() function to create and modify roles for WordPress 3.0.

// Use the function to create and modify roles
populate_roles_300();

Create a custom role after populating default roles

The following example demonstrates creating a custom role after calling the populate_roles_300() function to populate the default WordPress 3.0 roles.

// Populate default roles
populate_roles_300();

// Create a custom role
$custom_role = add_role('custom_role', 'Custom Role', array(
    'read' => true,
    'edit_posts' => true,
    'delete_posts' => false,
));

Modify a default role after populating roles

In this example, we will modify the capabilities of the ‘author’ role after calling the populate_roles_300() function to populate the default roles.

// Populate default roles
populate_roles_300();

// Get the author role
$author_role = get_role('author');

// Modify the author role capabilities
$author_role->add_cap('edit_published_pages');
$author_role->remove_cap('upload_files');

Reset roles to default

This example demonstrates how to reset WordPress roles to their default state using the populate_roles_300() function.

// Remove all roles
remove_all_roles();

// Populate roles to their default state
populate_roles_300();

Check if roles are populated before adding a custom role

In this example, we will check if roles are populated before adding a custom role using the populate_roles_300() function.

// Check if roles are populated
if (count(wp_roles()->roles) === 0) {
    // Populate default roles
    populate_roles_300();
}

// Add a custom role
$custom_role = add_role('custom_role', 'Custom Role', array(
    'read' => true,
    'edit_posts' => true,
    'delete_posts' => false,
));