Skip to main content

Importing disqus comments into WordPress

/galleries/dropbox/disqus2wp1.png

Long story short: this is not trivial.

Disqus is a popular service that provides commenting functionality. All you need to do for your HTML page to have embedded discussion is to add a bit of javascript.

They have awesome importers from various blog engines. This blog was originally hosted on Blogspot, migration to static blog generated by Octopress required me to search for alternative commenting facilities and I decided to use disqus (is there anything else, really?) Now I switched to WordPress and I wanted my comments back.

Disqus does provide the ability to export all the comments in a XML file. Quick Google search told me that it was possible to get the comments quite easily into WordPress by converting the disqus dump into WXR, but it is not really that easy. WordPress will ignore the post if it is already there and will not import comments. There’s a plugin that imports disqus comments, but I wanted full support of nested comments and little to no PHP coding.

I dumped all the wordpress database locally and hacked up a script that reads the comments.xml file and puts the necessary data into the database. The script needs access to some sort of a database in order to figure out the post_id for each post and generate the comment identifiers for correct nesting.

...
ANONYMOUS_EMAIL = 'nobody@example.net'

# Database configuration
DATABASE = {
    'host': 'lab.lappyfamily.net',
    'user': 'rtg',
    'name': 'rtginua6_wp1',
}

# Admin information
ADMIN_INFO = {
    'comment_author': 'Roman Yepishev',
    'comment_author_email': 'roman.yepishev@yandex.ua',
    'user_id': 1
}

# Names used in disqus that represent administrator
ADMIN_ALIASES = set(['rtg', 'Roman', 'rye'])
...

And the script itself runs on an uncompressed disqus XML. The script will skip all pages that it could not find the URLs for:

$ python wp-import-disqus.py comments.xml
Skipping comment for http://rtg.in.ua/app/acer-exif-fixup/index.html

After script finished I dumped the comments table and uploaded it via the phpmyadmin interface.

$ mysqldump -h lab rtginua6_wp1 wp_comments > wp_comments.sql

This resulted in all 140 comments being correctly imported in a correct encoding and properly nested.

If you happen to know a better way to import comments, feel free to provide the links to alternative solutions.

Comments

Comments powered by Disqus