Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions plugin-developer-learning-pathway/09-Users/01-Working-with-Users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
## Working with Users

WordPress provides a robust user management system that allows site owners and developers to manage users efficiently. Understanding how to work with users in WordPress can help improve site administration, enhance security, and customize user experiences. This guide covers the basics of user roles, user management, and how to programmatically interact with users using WordPress functions.


## Understanding WordPress User Roles

WordPress comes with a built-in user role management system, which defines what actions each user can perform. The default roles include:


- Administrator – Has full control over the site, including themes, plugins, and user management.

- Editor – Can manage and publish all posts, including those of other users.

- Author – Can publish and manage their own posts.

- Contributor – Can write and manage their own posts but cannot publish them.

- Subscriber – Can only manage their profile and read content.

## Managing Users in WordPress


## Adding New Users

You can add new users manually through the WordPress dashboard:


- Go to Users > Add New.

- Enter the required details such as username, email, and password.

- Assign an appropriate role.

- Click Add New User.

Alternatively, you can allow users to register via Settings > General, by enabling the Anyone can register option and setting a default role.


## Editing and Deleting Users

To edit user details:

- Navigate to Users > All Users.

- Click on a user’s name.

- Modify the relevant fields and click Update User.

To delete a user:

- Go to Users > All Users.

- Select the user and choose Delete.

Assign their content to another user if necessary.


## Programmatically Working with Users

For developers, WordPress provides various functions to manage users via code.

## Retrieving User Information

Use get_user_by() to retrieve user data:


```php
$user = get_user_by('email', '[email protected]');
if ($user) {
echo 'User ID: ' . $user->ID;
}
```
## Creating a New User

Use wp_create_user() to programmatically add a user:


```php
$user_id = wp_create_user('username', 'password123', '[email protected]');


if (!is_wp_error($user_id)) {
echo 'User created successfully!';
}
```
## Updating User Data

Use wp_update_user() to modify user details:


```php
wp_update_user(array(
'ID' => 2,
'first_name' => 'John',
'last_name' => 'Doe'
));
```
## Deleting a User

Use wp_delete_user() to remove a user:


```php
wp_delete_user(2);
```
## Checking User Capabilities

WordPress provides the current_user_can() function to check permissions:


```php
if (current_user_can('edit_posts')) {
echo 'You can edit posts!';
}
```
## Customizing User Roles and Capabilities

WordPress allows developers to create custom roles and assign specific capabilities.


## Creating a Custom Role

Use add_role() to create a new role:


```php
add_role('custom_role', 'Custom Role', array(
'read' => true,
'edit_posts' => true,
));
```
## Adding or Removing Capabilities

Use add_cap() or remove_cap() to modify capabilities:
```php
$role = get_role('editor');
$role->add_cap('manage_options'); // Grant ability to manage options
$role->remove_cap('publish_posts'); // Remove ability to publish posts
```
## Conclusion

WordPress provides a powerful user management system that allows site owners and developers to manage users efficiently. Whether through the dashboard or programmatically, understanding how to work with users in WordPress helps maintain site security, improve user experience, and customize roles to fit specific needs.