Wordpress User Data Export through API?

137 views Asked by At

Does anyone know if there is a way of exporting all you WP users through API? I tried WP Rest API but that only gives me the users who has is an author in the site. we use ultimate member plugin to organize and categorize the members, but their API does not look promising(more like useless)

Instead of manually export the user data, I am trying to find a way to export it through API, so everytime I call that API, it will just give the data.

1

There are 1 answers

0
Alen Joseph On

Custom Endpoint with WP REST API:

You can create a custom endpoint using the WordPress REST API to fetch the user data. This could involve creating a custom plugin or adding code to your theme's functions.php file.

function custom_user_export_endpoint() {
        register_rest_route('custom/v1', '/user-export/', array(
            'methods' => 'GET',
            'callback' => 'custom_user_export_callback',
            'permission_callback' => function () {
                return current_user_can('manage_options');
            },
        ));
    }
    
    function custom_user_export_callback($data) {
        $users = get_users(array('fields' => 'all'));
    
        // Process and format the user data as needed
        $formatted_users = array();
        foreach ($users as $user) {
            // Customize this based on the data you need
            $formatted_users[] = array(
                'ID' => $user->ID,
                'user_login' => $user->user_login,
                // Add more fields as needed
            );
        }
    
        return rest_ensure_response($formatted_users);
    }
    
    add_action('rest_api_init', 'custom_user_export_endpoint');

This example creates a custom endpoint /wp-json/custom/v1/user-export/ that returns user data. You can customize the fields and data to match your requirements.