Create code that exports all user data from their profile into a csv file to download.

Home » Snippets » Create code that exports all user data from their profile into a csv file to download.
0

Created with:

Visibility: 

public

Creator: Alexis Lecomte

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: User Data Export
 * Version: 1.0.0
 * Description: This plugin exports user data from their profile into a CSV file.
 */

// Register the export user data function to a custom hook
add_action('wp_loaded', 'wpturbo_export_user_data');

/**
 * Export user data to a CSV file and initiate the download.
 */
function wpturbo_export_user_data() {
    // Check if user is logged in and has the necessary capability
    if (is_user_logged_in() && current_user_can('manage_options')) {
        // Get all user data
        $users = get_users();
        
        // Prepare CSV data
        $csv_data = array();
        $csv_data[] = array('ID', 'Username', 'Email', 'First Name', 'Last Name');
        
        foreach ($users as $user) {
            $csv_data[] = array(
                $user->ID,
                $user->user_login,
                $user->user_email,
                $user->first_name,
                $user->last_name
            );
        }
        
        // Set headers to initiate download
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="user_data.csv"');
        
        // Create a file pointer
        $output = fopen('php://output', 'w');
        
        // Write CSV data to the file pointer
        foreach ($csv_data as $row) {
            fputcsv($output, $row);
        }
        
        // Close the file pointer
        fclose($output);
        
        // Stop further execution
        exit;
    }
}
				

Explanation:

This code snippet creates a WordPress plugin that exports user data to a CSV file when a specific hook is triggered. The wpturbo_export_user_data function is registered to the wp_loaded hook, which ensures that it runs when WordPress is fully loaded. Inside this function, we first check if the user is logged in and has the necessary capability (in this case, the capability to manage options).

If the user is authorized, we fetch all the user data using the get_users function. We then prepare the CSV data by creating a multidimensional array, where each inner array represents a row in the CSV file. The first row consists of the column headers: ID, Username, Email, First Name, and Last Name. We loop through each user and add their data to the CSV array.

Next, we set the appropriate headers for the HTTP response to initiate the file download. The Content-Type header is set to text/csv, indicating that the response is a CSV file. The Content-Disposition header specifies the filename and prompts the browser to download the file instead of displaying it inline.

We create a file pointer using fopen and write the CSV data to the file using fputcsv in a loop. Finally, we close the file pointer and exit to stop further execution.

Please note that this code assumes you have the necessary privileges to manage options and it only exports basic user data. You can customize the code further to include additional user data fields as needed.

Register an account to save your snippets or go Pro to get more features.