Using WordPress ‘populate_roles_210()’ PHP function

The populate_roles_210() WordPress PHP function creates and modifies WordPress roles for WordPress 2.1.

Usage

To use the populate_roles_210() function, simply call it without any parameters:

populate_roles_210();

Parameters

  • None

More information

See WordPress Developer Resources: populate_roles_210()

This function is specific to WordPress 2.1.

Examples

Creating default roles in WordPress 2.1

This example demonstrates how to create default roles using populate_roles_210().

// Call the function to create and modify roles
populate_roles_210();

Setting up a custom role

This example demonstrates how to create a custom role with capabilities using populate_roles_210() and the add_role() function.

// Call the function to create and modify roles
populate_roles_210();

// Add a custom role with capabilities
$wp_roles = new WP_Roles();
$wp_roles->add_role('custom_role', 'Custom Role', array(
    'read' => true,
    'edit_posts' => true,
));

Removing a default role

This example demonstrates how to remove a default role using populate_roles_210() and the remove_role() function.

// Call the function to create and modify roles
populate_roles_210();

// Remove the 'subscriber' role
$wp_roles = new WP_Roles();
$wp_roles->remove_role('subscriber');

Modifying a role’s capabilities

This example demonstrates how to modify a role’s capabilities using populate_roles_210() and the add_cap() and remove_cap() functions.

// Call the function to create and modify roles
populate_roles_210();

// Modify the 'author' role's capabilities
$author = get_role('author');
$author->add_cap('edit_others_posts');
$author->remove_cap('delete_posts');

Checking if a role exists

This example demonstrates how to check if a role exists using populate_roles_210() and the is_role() function.

// Call the function to create and modify roles
populate_roles_210();

// Check if the 'editor' role exists
$wp_roles = new WP_Roles();
if ($wp_roles->is_role('editor')) {
    echo "The 'editor' role exists!";
} else {
    echo "The 'editor' role does not exist!";
}