lundi 9 novembre 2015

Export User Info into CSV - Additional Column

I'm using the 'Export Users to CSV' a plugin for wordpress. By default, The plugin only extracts wp_users and wp_usermeta

I have been wanting to add post_title column from wp_posts. I was able to pull out the 'post_title' into the CSV file BUT it's not placed correctly in its respective row.

Please see my edited code and original code

Orignal version:

global $wpdb;

        $data_keys = array(
            'ID', 'user_login', 'user_pass',
            'user_nicename', 'user_email', 'user_url',
            'user_registered', 'user_activation_key', 'user_status',
            'display_name'
        );
        $meta_keys = $wpdb->get_results( "SELECT distinct(meta_key) FROM $wpdb->usermeta" );
        $meta_keys = wp_list_pluck( $meta_keys, 'meta_key' );
        $fields = array_merge( $data_keys, $meta_keys);

        $headers = array();
        foreach ( $fields as $key => $field ) {
            if ( in_array( $field, $exclude_data ) )
                unset( $fields[$key] );
            else
                $headers[] = '"' . strtolower( $field ) . '"';
        }
        echo implode( ',', $headers ) . "\n";

        foreach ( $users as $user ) {
            $data = array();
            foreach ( $fields as $field ) {
                $value = isset( $user->{$field} ) ? $user->{$field} : '';
                $value = is_array( $value ) ? serialize( $value ) : $value;
                $data[] = '"' . str_replace( '"', '""', $value ) . '"';
            }
            echo implode( ',', $data ) . "\n";
        }

        exit;
    }
}

Edited version

global $wpdb;

        $data_keys = array(
            'ID', 'user_login', 'user_pass',
            'user_nicename', 'user_email', 'user_url',
            'user_registered', 'user_activation_key', 'user_status',
            'display_name'
        );
        $meta_keys = $wpdb->get_results( "SELECT distinct(meta_key) FROM $wpdb->usermeta" );
        $meta_keys = wp_list_pluck( $meta_keys, 'meta_key' );
        $post_keys = $wpdb->get_results( "SELECT distinct(post_title) FROM $wpdb->posts" );
        $post_keys = wp_list_pluck( $post_keys, 'post_title' );
        $fields = array_merge( $data_keys, $meta_keys, $post_keys);

        $headers = array();
        foreach ( $fields as $key => $field ) {
            if ( in_array( $field, $exclude_data ) )
                unset( $fields[$key] );
            else
                $headers[] = '"' . strtolower( $field ) . '"';
        }
        echo implode( ',', $headers ) . "\n";

        foreach ( $users as $user ) {
            $data = array();
            foreach ( $fields as $field ) {
                $value = isset( $user->{$field} ) ? $user->{$field} : '';
                $value = is_array( $value ) ? serialize( $value ) : $value;
                $data[] = '"' . str_replace( '"', '""', $value ) . '"';
            }
            echo implode( ',', $data ) . "\n";
        }

        exit;
    }
}

I hope my explanation is not confusing. Cheers!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire