I have shops and consultants both set up on Wordpress as users with different roles. I want to include an "Export" link on the consultants' profiles using a shortcode that will export the shops on their list to evaluate to a CSV/Excel file. The consultants' display names have been added to the shop profiles as meta data, so I'm trying to call all of the shops that include their name. This is what I have so far, and I can't seem to figure it out from here.

  • The shortcode attribute 'bc' grabs the consultant's display name
  • $shop_query looks for all shops that match the consultant's display name
  • $lineData includes the meta-keys of the shop information that I want to include in the CSV file

Any help would be greatly appreciated. :)

function fc_pbestorelist_export_functions($atts) {
$atts = shortcode_atts( array(
    'bc' => do_shortcode('[display_name]'),
), $atts );         
    if($_GET['export'] == 'true'){
       $shop_query = new WP_User_Query( array( 
          'meta_key' => 'Group',
          'meta_value' => $atts['bc'],
          'role__in'    => wp_parse_list( ['shop','shop_co','shop_noco'] ),
       ) );    
       $query = $shop_query->get_results();

       $delimiter = ',';
       $filename = 'ShopList_' . date('Ymd') . '.csv'; // Create file name

       //create a file pointer
       $f = fopen('ShopList', 'r'); 

       //set column headers
       $fields = array('Username', 'Email', 'pbe_evaltype_month1', 'pbe_evaltype_month2', 'pbe_evaltype_month3');
       fputcsv($f, $fields, $delimiter);

       //output each row of the data, format line as csv and write to file pointer
       while($row = $query->fetch_assoc()){
          $lineData = array($row['nicename'], $row['user_email'], $row['pbe_evaltype_month1'], $row['pbe_evaltype_month2'], $row['pbe_evaltype_month3']);
          fputcsv($f, $lineData, $delimiter);

       //move back to beginning of file
       fseek($f, 0);

       //set headers to download file rather than displayed
       header('Content-Type: text/csv');
       header('Content-Disposition: attachment; filename="' . $filename . '";');

       //output all remaining data on a file pointer
    $results = '<a href=/user/?export=true>Export CSV</a>';
return $results;
add_shortcode( 'fc_pbestorelist_export', 'fc_pbestorelist_export_functions' );
  • 1
    This will never work as a shortcode because by the time shortcodes run the HTTP headers have already been sent. This shouldn't be implemented as a shortcode. Shortcodes are useful for swapping out content in posts with things that are too complex to put directly in post content or can't be put there for security reasons. Also your code has little or no indenting which makes it very difficult to read, can you update your code to include correct indentation? – Tom J Nowell Oct 1 '19 at 20:07
  • @TomJNowell, I am still learning how to do this stuff as I go, so please forgive me for not having the proper knowledge to complete this task. If a shortcode isn't the proper way to go, can you tell me how else I can achieve this? I updated the code to include what I believe to be correct indentation. Crossing my fingers – Mr Gandy Oct 1 '19 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.