You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何用WordPress默认函数wp_export()导出仅含用户名与邮箱的CSV

Solution for Exporting Username & Email as CSV (Bypassing wp_export())

Got it, let's break this down clearly: the wp_export() function is purpose-built for generating WordPress content XML exports—there's no way to force it to output CSV, and it doesn't support filtering down to just username/email data. Instead, we'll build a custom solution that does exactly what you need.

Step 1: Create a Custom Export Function

This function will fetch only the user data you want, format it as CSV, and trigger a download. Add this to your theme's functions.php file or a custom plugin:

function export_users_username_email_csv() {
    // Restrict access to admins only
    if (!current_user_can('manage_options')) {
        wp_die('You don\'t have permission to run this export.');
    }

    // Fetch users with only the fields we need
    $users = get_users(array(
        'fields' => array('user_login', 'user_email') // No extra data, keeps it lean
    ));

    // Set HTTP headers to trigger CSV download
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="user-credentials-' . date('Y-m-d') . '.csv"');
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');

    // Open output stream to write CSV
    $output = fopen('php://output', 'w');

    // Write CSV header row
    fputcsv($output, array('Username', 'Email Address'));

    // Loop through users and write each row
    foreach ($users as $user) {
        fputcsv($output, array($user->user_login, $user->user_email));
    }

    // Clean up and exit to prevent extra output
    fclose($output);
    exit;
}

Step 2: Add a Way to Trigger the Export

Let's add a simple admin menu item so you can run the export from your WordPress dashboard:

add_action('admin_menu', 'add_user_csv_export_menu_item');
function add_user_csv_export_menu_item() {
    add_users_page(
        'Export Username/Email CSV', // Page title
        'Export User CSV', // Menu label
        'manage_options', // Required permission
        'export-user-csv', // Menu slug
        'export_users_username_email_csv' // Function to run when page is loaded
    );
}

Why This Works (And Why wp_export() Doesn't)

  • wp_export() is hardcoded to generate XML for full WordPress content exports (posts, pages, users, etc.). There are no filters or parameters to switch it to CSV, or to limit it to just username/email.
  • Our custom approach uses get_users() with the fields parameter to fetch only the data we need, which is efficient and avoids loading unnecessary user metadata.
  • The HTTP headers ensure your browser recognizes the output as a downloadable CSV file, not a web page or XML.

Extra Tips

  • If you want to limit the export to specific user roles, add a role parameter to the get_users() array (e.g., 'role' => 'subscriber').
  • For larger user bases, you might want to add pagination or batch processing to avoid timeouts, but this code works great for most sites.

内容的提问来源于stack exchange,提问作者rescue1155

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:16:17