WordPress: import your Blogger posts using the built-in core functions

WordPress: import your Blogger posts using the built-in core functions

How to create an import script to move your posts from Blogger to WordPress.

If you have an old Blogger site and a new WordPress blog, you might need to import your Blogger posts into your new WordPress installation. WordPress already provides a specific tool for this kind of task, but sometimes things may go wrong and you need to proceed manually.

First of all, export your Blogger posts as an XML file. Then create a script which makes use of the built-in WordPress function wp_insert_post():


require_once('wp-load.php');

$blogger_file = simplexml_load_file('blogger.xml'); // your Blogger export file
$entries = $onwebdev->entry;

foreach($entries as $entry) {

	$published = strftime('%Y-%m-%d %H:%M:%S', strtotime($entry->published));
	$tag = $entry->category[1]['term'];
	$title = $entry->title;
	$content = $entry->content;
	
	wp_insert_post(array(
		'post_content' => $content,
		'post_date' => $published,
		'post_status' => 'publish',
		'tags_input' => $tag,
		'post_title' => $title,
		'post_author' => 1 // Your WordPress user ID
	));


}

The above script should be executed inside the root directory of your site. The first line includes all the WordPress core functions, so just make sure to specify the correct path.

A word of caution: the script might suffer from performance leaks, especially when your Blogger import file is huge in size. For that reason, you can choose to use another PHP approach to parse the XML file, such as XMLReader or another stream-based parsing utility.

Another thing: the script only adds tags to each inserted post. If you need categories, you have to perform an additional routine to check whether the category element of the XML file contains terms which match the existing categories already created on your WordPress blog.

Alternatively, you can later use the built-in WordPress category converter to turn tags into categories. Be aware, however, that if your Blogger file contains many tags, you'll end up with having an oversized WordPress taxonomy structure with several categories which may contain only one post per tag.