Featured image of post Python Script to Retrieve All User Information from a WordPress Site

Python Script to Retrieve All User Information from a WordPress Site

The WordPress /wp-json/wp/v2/users endpoint is used to retrieve all user information from the website. The response from this endpoint includes...

The WordPress /wp-json/wp/v2/users endpoint is used to retrieve all user information from a website. The response from this endpoint contains the following fields:

  • id: User ID
  • name: User’s name
  • email: User’s email address
  • url: User profile URL
  • avatar: User avatar URL
  • roles: User roles
  • registered: User registration date
  • last_login: User’s last login date
  • meta: User metadata

Below is an example of a Python script that retrieves all user information from a WordPress site:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests

# Send GET request to fetch user data
response = requests.get('http://your-wordpress-url/wp-json/wp/v2/users')

# Check if the request was successful
if response.status_code == 200:
    users = response.json()  # Parse JSON data
    total_users = len(users)  # Get number of users
    print(f"Total Users: {total_users}\n")

    # Iterate through each user
    for user in users:
        print("User ID:", user.get('id', 'N/A'))
        print("Name:", user.get('name', 'N/A'))
        print("Username:", user.get('username', 'N/A'))
        print("Email:", user.get('email', 'N/A'))
        roles = user.get('roles', [])
        if roles:
            print("Roles:", ', '.join(roles))
        else:
            print("Roles: N/A")
        avatar_urls = user.get('avatar_urls', {})
        if avatar_urls:
            print("Avatar URLs:")
            for size, url in avatar_urls.items():
                print(f"- {size}: {url}")
        else:
            print("Avatar URLs: N/A")
        print("Description:", user.get('description', 'N/A'))
        print("Profile Link:", user.get('link', 'N/A'))
        print("----------------------")
else:
    print("Failed to retrieve user data. Status code:", response.status_code)

By default, this endpoint returns information for all users, including usernames. If you wish to hide usernames, you can use WordPress’s show_user_logins setting.

For security reasons, you should restrict access to this endpoint. You can use the WordPress rest_user_query filter to limit which users can access the endpoint.