Create a CSV Export in Wordress
- سُلَيْمَان بْن دَاوُوْد

- Jun 21, 2020
- 1 min read
This code creates a sample plugin that can export users from word press based on roles
So logically the hook is as below
add_action( 'admin_post_{action_name}', 'action_name_callback' );and the action name need to be specified as hidden field on form in backend
<input type="hidden" name="action" value="action_name"><?php/*Plugin Name: Member exportAuthor: Nazrul HassanDescription: Export Usersversion:1.0*/if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directlyfunction export_member(){ add_menu_page('Export Member Title', 'Member Member ', 'manage_options', 'export-member', 'export_member_callback','dashicons-awards'); }add_action('admin_menu', 'export_member');function export_member_callback(){?> <form action="<?php echo admin_url(); ?>/admin-post.php" method="post"> <input type="hidden" name="action" value="export_member_csv1"> <hr> <input type="submit" value="Export Users" class="button button-primary button-large"> </form> <?php}add_action( 'admin_post_export_member_csv1', 'export_member_csv_callback' );function export_member_csv_callback() { if ( ! current_user_can( 'manage_options' ) ) return; $csv_filename = 'member_'.date('D-M-d-Y-H:i:s-A-e',time()).'.csv' ; header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename='.$csv_filename); header('Pragma: no-cache'); header('Expires: 0'); $file = fopen('php://output', 'w'); // send the column headers $csvheaders = array( 'S.No', 'User ID', 'Email', 'First Name', 'Last Name', ); fputcsv($file, $csvheaders ,','); $args = array( 'role__in' => array('Editor') , ); $user_query = new WP_User_Query($args ); $users = $user_query->get_results(); $i=1; foreach ( $users as $user ) { $email = $user->data->user_email ; $fname = get_user_meta($user->id,'first_name',true); $lname = get_user_meta($user->id,'last_name',true); $csvcolumns = array( $i, $user->data->ID, $email, $fname, $lname ) ; fputcsv($file,$csvcolumns); $i++; } exit();}
Comments