• This new version is more performant, automatically ignore some fields that are default in wordpress and fix the issue of the blank page.

    <?php
    /*
    Plugin Name: Export Users to CSV
    description: Export Users to CSV Plugin allows you to export users list and their metadata in CSV file.
    Version: 1.4
    Author: Boopathi Rajan
    Author URI: https://www.boopathirajan.com
    License: GPLv2 or later
    License URI: https://www.gnu.org/licenses/gpl-2.0.html
    */
    
    function eu_register_export_page() {
        add_submenu_page('tools.php', 'Export Users', 'Export Users', 'manage_options', 'wp-export-users', 'eu_export_users_page');
    }
    add_action('admin_menu', 'eu_register_export_page');
    
    function eu_export_users_page() {
    }
    
    add_action('admin_init', 'eu_export');
    
    function eu_export() {
        if (!isset($_GET['page']) || isset($_GET['page']) && $_GET['page'] !== 'wp-export-users') {
            return;
        }
        global $wpdb, $table_prefix;
        ob_end_clean();
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="' . time() . '.csv";');
        $file = fopen('php://output', 'w');
        $ignore_sql = ' WHERE ';
        foreach (array(
            'meta-box',
            'metabox',
            'closedpostboxes',
            'dashboard',
            'nag',
            'managenav',
            'password',
            'ssl',
            'dismissed',
            'admin',
            'token',
            'jetpack',
            'menu',
            'elementor',
            'wpcom',
            'submit',
            'syntax',
            'panel',
            'shortcut',
            'editing',
            $table_prefix
        ) as $ignore_me) {
            $ignore_sql .= "meta_key NOT LIKE '%" . $ignore_me . "%' AND ";
        }
        $meta_sql = 'SELECT meta_key FROM ' . $wpdb->usermeta . $ignore_sql . " meta_key!='first_name' AND meta_key!='last_name' GROUP BY meta_key";
        $users = get_users( array( 'orderby' => 'ID','order' => 'ASC' ) );
        $meta_keys = $wpdb->get_results($meta_sql);
        $datas = array();
        $headers = array(
            'ID',
            'Username',
            'Email',
            'Display Name',
            'First Name',
            'Last Name',
            'Registered Date'
        );
        if ($users) {
            foreach ($users as $user) {
                $user_meta = get_userdata($user->ID);
                $user_data = array();
                $user_data[] = $user->ID;
                $user_data[] = $user->user_login;
                $user_data[] = $user->user_email;
                $user_data[] = $user->display_name;
                $user_data[] = $user_meta->first_name;
                $user_data[] = $user_meta->last_name;
                $user_data[] = $user->user_registered;
                if (!empty($meta_keys)) {
                    foreach ($meta_keys as $meta_key) {
                        $key = $meta_key->meta_key;
                        if (!isset($headers[$key])) {
                            $headers[$key] = $key;
                        }
                        if (is_object($user_meta->$key) || is_array($user_meta->$key)) {
                            if (is_array($user_meta->$key) && count($user_meta->$key) == 1 && isset($user_meta->$key[0])) {
                                $user_data[] = $user_meta->$key[0];
                            }
                            else {
                                $user_data[] = serialize($user_meta->$key);
                            }
                        }
                        else {
                            $user_data[] = $user_meta->$key;
                        }
                    }
                }
                $datas[] = $user_data;
            }
        }
    
        fputcsv($file, $headers);
        foreach ($datas as $data) {
            fputcsv($file, $data);
        }
        fclose($file);
        exit();
    }
    
    /* Add action links to plugin list*/
    add_filter('plugin_action_links_' . plugin_basename(__FILE__) , 'eu_add_export_user_action_links');
    function eu_add_export_user_action_links($links) {
        $settings_link = array(
            '<a href="' . admin_url('tools.php?page=wp-export-users') . '">Export Users</a>'
        );
        return array_merge($links, $settings_link);
    }
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘New improved version’ is closed to new replies.