18 04 2012
How to Import Squarespace into WordPress
Recently I had to convert an entire squarespace website to wordpress. There is no official way to do this and some pages do require custom scripts/scrapers however it is possible to convert the squarespace posts easily.
New WordPress plugin and how-to video
Update: I have a written a WP plugin to do this, click here to view the squarespace to wordpress plugin post. There is also an instructional video.
Otherwise, you can use the more advanced method, outlined below.
First, login to your square space website, click on structure > configure this page. Go down to Data Export and Export Blog Data to get a backup of your posts. This only works for squarespace posts not other content.
Next go to your wordpress installation, click Tools > Import. Install the Movable Type and TypePad post importer plugin. Now run this plugin on your exported squarespace posts and they will import into wordpress. It would be wise to create a new user when importing these posts so they are easy to identify.
So now that you have your squarespace posts imported into wordpress, you may have noticed all of the images are referring to square space still. You will see html code such as:
What we need to do is run a script that will go through our imported posts, download the squarespace images, rename them, upload them into wordpress, add them as attachments to the posts they belong to and change the html to reflect this.
Sounds complicated doesn’t it? Click below to download my quick solution:
The tools run on linux/unix command line, it uses wget to download the file so you must have that installed. Explanation on how to install wget and run php scripts on the command line is beyond the scope of this post. Please do this on a local/backup copy of wordpress unless you are sure you know what you are doing and as always, make a backup first. This script also uses the php function shell_exec to run wget, some shared hosts may disable this, again it’s better to do this on a local development environment.
Open config.php and set these constants. The squarespace prefix is important for use in a later script. The download path (./images in the current directory by default) must have write permissions for your user or just chmod 777.
define(‘SQUARESPACE_URL’, ‘http://www.mysquarespacesite.com’); // no trailing slash
define(‘YOUR_WP_DIR’, ‘/path/to/wp/dir’); // no trailing slash
define(‘YOUR_DOWNLOAD_PATH’, ‘./images’); // no trailing slash
Execute the script on the command line:
[crayon lang=”bash”]php download_images.php[/crayon]
The terminal output looks like this:
This script is…
- Looping over your posts and finding squarespace images
- Downloading the squarespace images to the directory YOUR_DOWNLOAD_PATH
- Renaming them based off the post title slug
- Replacing the html of the wordpress post to reference this new link
So now we have downloaded the squarespace images and changed the html but we are not done yet. For starters we need to import those images into wordpress and then change the image src again to match the fully qualified wordpress url. The reason we did not do this in the last script is a perfectly good plugin already exists to do this upload. Install Add From Server plugin into your wordpress install: http://wordpress.org/extend/plugins/add-from-server/
Once Add from server is installed use it to upload all of the images downloaded from squarespace. It’s under the media tab in the wordpress admin. You will need to copy your downloaded images folder to wp-content.
Now that Add from server has uploaded your squarespace images and created the attachment data, we are going to link these attachments with their posts and update the image srcs to reflect the attachment urls.
[crayon lang=”bash”]php set_attachments.php[/crayon]
- Loops through your wordpress posts
- Find all images in a post
- Checks to see if the image begins with your SQUARESPACE_PREFIX
- If it does, it associates the attachment ID with the current post
- Sets the first matched image to your featured image
- Updates the image URL to be the fully qualified wordpress attachment URL
It looks like this:
So there you have it, you’ve imported squarespace posts with images too! Hopefully this will be helpful to some people.
So now, in your wordpress post content, instead of the squarespace image links you will see:
You could write other script/conversion tools to cleanup the remaining squarespace html. I had to write several mini scripts such as the ones above to import things like squarespace galleries and other custom content types.
If enough people need this, I could start writing a proper wordpress import plugin, or at least combine the three steps into one script. This approach could more or less be used for converting any site to wordpress that doesn’t have an official importer, so feel free to modify it.
Update: There is now a wordpress plugin for this: click here to download the squarespace to wordpress plugin.