0

I'm trying to alter a plugin (WP Log Action) to allow for exporting of a table in the WP Admin area. Alas, instead of a CSV file I get the results printed to the screen and the classic old "WARNING: Cannot modify header information - headers already sent by ..." etc message.

I've looked at other questions on this topic and it seems like hooking into admin_init does the trick for people but I'm having trouble. Perhaps because the function that is doing the exporting in my case is a class that extends WP_List_Table?

Here's my code: (I've tried hooking into admin_init both at the beginning of the file and within the class itself in the _construct function.

<?php
add_action('admin_init','handle_actions'); // This isn't working...

//Our class extends the WP_List_Table class, so we need to make sure that it's there
if(!class_exists('WP_List_Table')){
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}


/**
 * A WP List Table for displaying the options.
 * use http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/
 */
class WPLA_Options_Table extends WP_List_Table {

    /**
     * Constructor, we override the parent to pass our own arguments
     * We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
     */
     function __construct() {
         parent::__construct( array(
        'singular'=> 'wp_list_text_link', //Singular label
        'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
        'ajax'  => false //We won't support Ajax for this table
        ) );
        add_action('admin_init','handle_actions'); // This isn't working either...
     }

    // Blah blah blah code

    /**
     * Handle any bulk actions using current_action.
     */
    function handle_actions() {
        global $wpdb;
        $wpla = WPLA_Logger::get_instance();
        switch ($this->current_action()) {
            case 'export':

                    $jm_wpla_table_name = $wpdb->prefix . 'wpla_logs';

                    ob_end_clean();
                    $sql = $wpdb->get_results( "SELECT * FROM $jm_wpla_table_name");

                    if (!$sql) {
                        die('Invalid query: ' . mysql_error());
                    }

                    // Get The Field Name
                    $output .= 'Date'. ',';
                    $output .= 'Type'. ',';
                    $output .= 'Label'. ',';
                    $output .= 'Value'. ',';

                    $output .="\n";

                    // Get Records from the table

                    foreach ($sql as $row) {

                    $output .="\n";
                    $output .='"'.$row->log_time.'",';
                    $output .='"'.$row->log_type.'",';
                    $output .='"'.$row->log_label.'",';
                    $output .='"'.$row->log_value.'",';

                    }
                    $output .="\n";
                    // Download the file

                    $file = "wpla_logs";
                    $filename = $file."_".date("Y-m-d_H-i",time());
                    header("Content-type: application/vnd.ms-excel");
                    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                    header( "Content-disposition: filename=".$filename.".csv");

                    echo $output;
                    exit;                

            break;
            // other cases, blah blah          
        }
    }

Any insight? Thanks!

  • Have a look at the docs for add_action for examples of how to use it correctly with a class method. – Milo Jul 6 '17 at 3:38
  • Thanks for the lead. Looks like I should be using add_action('admin_init', array( $this,'handle_actions')); within the _construct function, but I'm still getting the same issue... – Lime Jul 6 '17 at 7:45

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.