Easily import sitewide comments – WPMU

One of my work responsibilities is the administration and development of a WordPress MU installation, Puget Sound Blogs. Someone suggested adding a feed of the most recent comments onto the front page, but obviously this isn’t terribly easy to do with WPMU, unless you have the sitewide comments plugin. http://wpmudev.org/project/Sitewide-recent-comments

Now the problem with this plugin becomes: What do I do to import my old comments. It’s fairly easy to simply export and re-import the comments, but I found this to be rather time consuming. So I wrote this script to do it all for me. Keep in mind that you’ll need to change which tables you use if they are different and whatnot, but it sure saved me a lot of time anyway.

mysql_connect('HOST', 'USER', 'PASS') 
  or die('Could not connect to DB');
mysql_select_db('DB') or die('No DB!');
$blogids = mysql_query("SELECT blog_id 
  FROM wp_blogs WHERE `deleted` != 1");

while($row = mysql_fetch_assoc($blogids))
$id = $row['blog_id'];

$q1 = "DELETE FROM `sitewide_comments` WHERE blog_id = '{$id}'";

$q2 = "INSERT INTO `sitewide_comments` SELECT NULL as `site_cid`,
'{$id}' as `blog_id`, 
`comment_ID`, `comment_post_ID`, 
`comment_author`, `comment_author_email`, 
`comment_author_url`, `comment_author_IP`,
`comment_date`, `comment_date_gmt`, 
`comment_content`, `comment_karma`,
`comment_approved`, `comment_agent`,
 `comment_type`, `comment_parent`, 
`user_id` FROM wp_{$id}_comments;";

echo "Syncing $id...
"; mysql_query($q1); mysql_query($q2); } echo 'done...';

One Response to “Easily import sitewide comments – WPMU”