In today's guide, you will get access to some code that imports comments exported from WordPress CMS and exports them into Disqus. If you have been previously hosting a website using WordPress CMS and you have decided it's time to convert your website into.NET or Umbraco you will need to perform a content migration off of WordPress. One way to do this is within the WordPress editor. If the URLs of your pages are not changing, you should be able to import your comments into Disqus directly from WordPress CMS. YOu can then link to Discus from your new website and they should hook up magically 😍.
Sometimes a simple import/export is not possible. If the website URL structure is changing, it may be easier to manually import comments into Disqus using the Disqus API. Using the Disqus API to import comments will allow you to add extra meta-data during the process. You can add a unique ID and Url. The unique ID will allow you to map comments correctly when the URL structure changes. So without further ado, let's crack on.
Exporting Comments Out Of WordPress CMS
This post assumes that you have exported your comments from WordPress using the plug-in, Export comment. This plug-in will allow you to export all your websites comments into a CSV file. For this tutorial, I will use an MVC controller and view to read in that CSV file, add data into a view model and then iterate through that view model to create an XML file that writes that data in the WXR format that Disqus requires to import it. You could additionally add this code to an app, or a scheduled task to do this instead. The choice is yours 👌!
In terms of code, the first step is to create a model that represents the format Discuq expects. This model itself is pretty straightforward:
Next, let us cover the conversion code. This is where the logic that reads in the CSV file and converts it into our model will live:
I'm hoping the code above is pretty simple to understand. One thing to note is that I'm using the VisualBasic library called TextFieldParser to do the CSV conversion. Although this is using VB we do not need to write any, do not panic 😱 I am using this library as it's simple to use. The only caveat is that you will need to add a reference to Visual Basic, yuk!
I'm also assuming in the code that when you exported your blog posts from WordPress CMS, you also imported the WordPress page Id. If you didn't then that will make things a lot more complicated. Basically, you need a unique ID to link a comment to a page. If your page structure is changing you can not use URL. In this instance, it makes sense to use the WordPress CMS page Id as this linking Id. The caveat is that you need access to that ID from each web page. If you find yourself in this situation, I suggest you import the WordPress Id when you import the blog content into your new site. To clarify, in this code I am using Umbraco CMS as the platform I am importing into. I have stored the Wordpres ID within a document type called documentTypeToImportToAlias
within a property called PostId
. This code will take the comments exported from WordPress CMS and convert them into a C# object. Next, we need to take that object and render an XML file we can import intoDiscus . This is done in a razor view. My view is called disqus.cshtml
:
The view code above is also pretty simple. I went to the Disqus import support page and copied the XML format, found here. The code iterates through all of my comments building up the elements and inserting the relevant bits of data within the correct tags. Remember you need that unique identifier that you can send to Discus from your web pages as well! In my code, this is done using this property comment.UmbracoId
When you this code an XML should get created. This file should contain all your comments on it, in a format that Disqus will understand. Be careful not to export the file as HTML. I recommend copying the tags by selecting view page source in a browser, copy and paste your XML and save it. An example of my final XML looked like this:
To import the XML file into Disqus, head to import.disqus.com. From here you can upload the file as well as check on import progress. Some useful things to note first. Make sure your file is a valid XML. The WordPress export plug-in can sometimes add funky characters to your content. You can check if the XML file is valid using the free W3C validation service here. if it is not you may need to cleanse the file manually until it passes.
Assuming you have the unique ID set up, you should be able to add the Discuss tag to your blog post page, pass in the ID to discuss and see the correct comments. Happy Coding 🤘