Export -> Download Export File
// - Edit the script and input your general parameters
// - Run the script by invoking its url with the following query string: ?exec=true
//
// Updates:
// - 1.3: Updated comment posting code to work with changes on Disqus's side
// - 1.2: Post and Comment content is now formatted in paragraph style like wordpress
// - 1.1: Some WordPress exports were invalid XML so DOM based parsing was replaced
// with parsing code directly from WordPress's own Import function
//
// If you like the script use it and drop me comments! If you hate the script
// fix it and or drop me comments! The script is available for free and to be
// modified under the above mentioned license. Please attribute the original
// and modified versions to the respective authors.
//
// dbavaria@gmail.com
//
$wp_import = new wp2tumblr();
//************************
//START GENERAL PARAMETERS
//************************
$wp_import->wordPressXmlURL = "wordpress.xml"; // LOCAL path to wordpress export file
$wp_import->tumblrName = "you"; // http://(you).tumblr.com
$wp_import->tumblrEmail = "you@mail.com"; // tumblr login
$wp_import->tumblrPassword = "password"; // tumblr password
$wp_import->disqusName = "you"; // http://(you).disqus.com
$wp_import->transferComments = true; // Transfer Comments to Disqus
//***********************
//END GENERAL PARAMETERS
//***********************
$wp_import->parsePosts();
$wp_import->transferPosts();
class wp2tumblr {
// account specific paramters
var $tumblrName;
var $tumblrEmail;
var $tumblrPassword;
var $disqusName;
// other parameters
var $wordPressXmlURL;
var $transferComments;
// post storage
var $rawPosts = array();
var $posts = array();
function transferPosts()
{
foreach($this->posts as $post){
//Create POST data for tumblr
$tumblrPostData = http_build_query(array('email' => $this->tumblrEmail,
'password' => $this->tumblrPassword,
'date' => $post['post_date'],
'title' => $post['post_title'],
'body' => $post['post_content'],
'type' => 'regular',
'generator' => 'WordPress to Tumblr',
));
//POST content to tumblr and return new postId
$tumblrPostId = $this->postHandler("http://www.tumblr.com/api/write/", $tumblrPostData);
$tumblrPostId = trim($tumblrPostId);
if (!is_numeric($tumblrPostId)) {
die("POST returned invalid result: $tumblrPostId");
}
echo "$number
WordPress " . $post['post_ID'] . " -> Tumblr $tumblrPostId
";
if (sizeof($post['commentdata']) > 0 && $this->transferComments == true) {
//Get the disqus 'threadSlug' - comment thread identifier for a particular blog post
$disqusThreadSlug = $this->getDisqusSlug("http://$this->tumblrName.tumblr.com/post/$tumblrPostId", $this->disqusName);
if ($disqusThreadSlug=="") {
die("Failed to get a valid disqus threadSlug!");
}
$commentNum = 0;
foreach($post['commentdata'] as $comment){
//Create POST data for disqus
$disqusPostData = http_build_query(array(
'create_user' => 'Choose+Username+*',
'create_password' => 'Choose+Password+*',
'login_user' => 'Email+or+Username',
'login_password' => 'Password',
'from_embed' => '1',
'to_redirect' => '',
'parent' => "http://$this->tumblrName.tumblr.com/post/$tumblrPostId",
'author_name' => $comment['comment_author'],
'author_email' => $comment['comment_author_email'],
'author_url' => $comment['comment_author_url'],
'message' => $comment['comment_content']
));
//POST comment to disqus
$this->postHandler("http://disqus.com/forums/$this->disqusName/$disqusThreadSlug/", $disqusPostData);
$commentNum++;
echo "Comment $commentNum of " . sizeof($post['commentdata']) . " posted to Disqus
";
}
}
echo "
";
}
}
function postHandler($url, $queryString)
{
//POST data to a URL using CURL
$session = curl_init($url);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $queryString);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$retVal = curl_exec($session);
curl_close($session);
return $retVal;
}
function getDisqusSlug($blogPostURL, $disqusName)
{
//Load disqus's thread.js for particular blog post
$url = "http://disqus.com/forums/$this->disqusName/thread.js?url=$blogPostURL&message=&title=";
$retVal = $this->postHandler($url, "");
//Parse thread.js and return the "threadSlug"
$regexp = "&f=$this->disqusName&t=(.*)&to_redirect=";
$re1='(\'\\?slug=\')'; # Single Quote String 1
$re2='(\\s+)'; # White Space 1
$re3='(\\+)'; # Single Character 1
$re4='(\\s+)'; # White Space 2
$re5='(\\\'.*?\\\')'; # Single Quote String 2
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $retVal, $matches))
{
$slug = $matches[5][0];
$slug = str_replace("'", "", $slug);
return $slug;
}
}
function parsePosts()
{
$this->get_entries();
$this->process_posts();
}
//Source: http://www.php.net/html_entity_decode
function unhtmlentities($string)
{
// replace numeric entities
$string = preg_replace('~([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~([0-9]+);~e', 'chr("\\1")', $string);
// replace literal entities
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
//All code below is a hacked up version of WordPress 2.3.3
//Source: \wordpress\wp-admin\import\wordpress.php
function get_tag($string, $tag )
{
preg_match("|<$tag.*?>(.*?)$tag>|is", $string, $return);
$searchCDATA = array('');
$replaceCDATA = array('', '');
$return = str_ireplace($searchCDATA, $replaceCDATA, $return[1]);
$return = trim($return);
return $return;
}
// Ugly mess that wordpress uses to auto-paragraphize post content
function wpautop($pee, $br = 1) {
$pee = $pee . "\n"; // just to make things a little easier, pad the end
$pee = preg_replace('|
\s*
|', "\n\n", $pee);
// Space things out a little
$allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(' . $allblocks . '>)!', "$1\n\n", $pee);
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "
$1
\n", $pee); // make paragraphs, including one at the end $pee = preg_replace('|\s*?
|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace $pee = preg_replace('!([^<]+)\s*?((?:div|address|form)[^>]*>)!', "
$1
$2", $pee); $pee = preg_replace( '||', "$1
", $pee ); $pee = preg_replace('!
\s*(?' . $allblocks . '[^>]*>)\s*
!', "$1", $pee); // don't pee all over a tag $pee = preg_replace("|(
]*)>|i', "', $pee); $pee = preg_replace('!', '", $pee); $pee = str_replace('
\s*(?' . $allblocks . '[^>]*>)!', "$1", $pee); $pee = preg_replace('!(?' . $allblocks . '[^>]*>)\s*
!', "$1", $pee); if ($br) { $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", ")(.*?)!is', 'clean_pre', $pee ); $pee = preg_replace( "|\n$|", '', $pee ); return $pee; } function get_entries() { set_magic_quotes_runtime(0); $this->rawPosts = array(); $num = 0; $doing_entry = false; $fp = fopen($this->wordPressXmlURL, 'r'); if ($fp) { while (!feof($fp) ) { $importline = rtrim(fgets($fp)); if (false !== strpos($importline, '