How to export comments in WordPress?

Option 1

As the article linked by @MikeMadern suggests:

  • in your web host control panel, go to PHPMyAdmin

  • select the table wp_comments

  • select Export, configure the format

    export mysql

  • further down in the same screen, select the Save as file and Go

    enter image description here

Option 2

Or, as the same article suggests, just use a plugin:

Export comments

Pulls comments out of your WordPress database for backups or analysis.

If you have WP_DEBUG enabled, the plugin dumps a couple of warnings, but it’s nothing critical, and it works as expected.

Option 3

Just retrieved an old script and adapted to WordPress (using the query from Export Comments as example). Put the file in the root of WP installation and access directly, it will export an Excel file with the comments.

It would be much better to have this as a plugin with security checks and all.
Remove from server after using. Or maybe put it in a folder as index.php and set an .htaccess password.

<?php
define( 'WP_USE_THEMES', false );
require( './wp-load.php' );

global $wpdb;
$query = "SELECT * FROM $wpdb->comments 
    WHERE 1 = 1 
    AND ( comment_approved = '1' OR comment_approved = '0' ) 
    ORDER BY comment_ID DESC";

$error = "Error: the query failed...
    <pre style="width:700px;word-wrap:break-word;white-space:normal;">$query</pre>";

$result = $wpdb->get_results( $query, ARRAY_A ) or wp_die( $error );

$header = array_keys( $result[0] );
$html = array();
$html[] = "<tr><td>" .implode( "</td><td>", $header ) . "</td></tr>";

foreach( $result as $row )
{
    $html[] = "<tr><td>" .implode( "</td><td>", $row ) . "</td></tr>";
}

$html = "<table>" . implode( "\r\n", $html ) . "</table>";

$fileName="Comments_" . date("Ymd") . '.xls';
header( "Content-type: application/vnd.ms-excel" ); 
header( "Content-Disposition: attachment; filename=$fileName" );

echo $html;
exit();

Leave a Comment