I am working with a WordPress plugin that outputs a table of data on the plugin’s admin page. I have modified the plugin to add a button on the page that exports the table of data into a CSV file. Everything works perfectly on my localhost, but after transferring to the live server, it no longer works - the CSV file that is exported is blank. I am using PHP Simple HTML DOM Parser to parse the HTML table and convert it to CSV, and this seems to be where the roadblock is happening.

I have added the export button with this code (parts of the URL hidden for privacy):

<form action="**website-url**/wp-content/plugins/**plugin**/includes/votes-logs-export.php" method="POST">
        <input type="submit" value="<?php _e('Export Table as CSV','**plugin**'); ?>" class="button" id="export" name="export" />
</form> 

My file votes-logs-export.php has this code:

require_once('../../../../wp-load.php');
include 'simple_html_dom.php';
$file_name = "contest_votes_".date('d-m-Y-H-i-s').'.csv';
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$file_name);
global $wpdb;
$export_sql = "SELECT log.*,pst.post_title FROM ".$wpdb->prefix."votes_tbl as log LEFT JOIN ".$wpdb->prefix."posts as pst on log.post_id=pst.ID ORDER BY pst.post_title DESC";
$export_logs = $wpdb->get_results($export_sql, OBJECT);
if (!empty($export_logs)) {
    $fp = fopen("php://output", "w");
    if ($wpdb->num_rows > 0) {
        $output = '';
        foreach ($export_logs as $logs) {
            $vote_id       = $logs->post_id;                    
            $postdata      = get_posts($vote_id);                 
            $vote_author_id = $postdata[0]->post_author;
            $vote_author   = get_the_author_meta( 'display_name', $vote_author_id );
            $voter_name    = $logs->ip;
            if(filter_var($voter_name, FILTER_VALIDATE_IP) !== false)
                $voter_name = $logs->ip;
            else
                $voter_name = get_the_author_meta( 'display_name', $voter_name );
            $voted_date    = $logs->date;
            $output .= '
                <tr>
                <td>'.$logs->post_title.'</td>
                <td class="author column-author">'.$vote_author.'</td>
                <td class="author column-author">'.$voter_name.'</td>
                <td class="author column-author">'.$voted_date.'</td>
                </tr>';
        }
    }
    $table = '
        <table>
        <tr>
        <th>Title</th>
        <th>Author</th>
        <th>Voter</th>
        <th>Vote Date</th>
        </tr>' . $output . '</table>';
    $html = str_get_html($table);
    foreach($html->find('tr') as $element){
        $td = array();
        foreach( $element->find('th') as $row){
            $td [] = $row->plaintext;
        }
        // prevent blank rows being inserted into CSV file
         if (!empty($td)) {
            fputcsv($fp, $td);
        }
        $td = array();
        foreach( $element->find('td') as $row){
            $td [] = $row->plaintext;
        }
        // prevent blank rows being inserted into CSV file
         if (!empty($td)) {
            fputcsv($fp, $td);
        }
    }
    fclose($fp);
}

I’m able to view the PHP page in order to debug by commenting out the header redirects. On my localhost, the PHP page outputs the table data in CSV format, but on the live server, it’s a blank page. (No errors on the page or in error_log.) I have debugged this code and found that $table and all of the code above it seems to be running perfectly - if I print the contents of $table I get a formatted HTML table of my data:

<table>
        <tbody><tr>
        <th>Title</th>
        <th>Author</th>
        <th>Voter</th>
        <th>Vote Date</th>
        </tr>
                <tr>
                <td>Writing-on-Stone Park</td>
                <td class="author column-author">admin</td>
                <td class="author column-author">email@email.com</td>
                <td class="author column-author">2014-07-04 16:42:57</td>
                </tr>
                <tr>
                <td>White Tailed Ptarmigan</td>
                <td class="author column-author">admin</td>
                <td class="author column-author">email@email.com</td>
                <td class="author column-author">2014-07-04 06:07:51</td>
                </tr>
                <tr>
                <td>Rockbound Lake</td>
                <td class="author column-author">admin</td>
                <td class="author column-author">email@email.com</td>
                <td class="author column-author">2014-07-04 06:07:28</td>
                </tr>
                </tbody></table>

The block happens at $html = str_get_html($table) - if I print out $html, nothing gets printed to the page, not even an error or any kind of ‘false’ or ‘NULL’ value. If I try to echo a string after that block of code, it is not output to the page, so the script seems to stop running there.

I was able to confirm that my simple_html_dom.php file is being referenced correctly because echoing a string in that file does get output to my votes-logs-export.php page. Yet the functions within the file don’t seem to be firing? I’m out of ideas on this one!

I confirmed that ‘allow_url_fopen’ is set to ‘true’ in php.ini. In my simple_html_dom.php file, I also tried changing the “MAX_FILE_SIZE” from 600,000 to 100,000,000 but to no effect. The memory on the server is set to 40MB. I admit I am a newbie with working with custom WordPress plugins, but the fact this works on my localhost seems to indicate there is some kind of configuration or compatibility issue on the live server…? I would appreciate any guidance or tips you can offer.

PHP version on the live server is 5.4.21. My localhost is using 5.3.

Read more here: PHP Simple HTML DOM Parser works on localhost but not live WordPress website


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , , , , ,

Wordpress related questions and answers: