/ Published in: PHP

URL: http://shakefon.tumblr.com
Recently moved my Wordpress blog to a tumblr account while I was removing a hosting account, wrote this script to do the transfer for me using Wordpress' Export feature and Tumblr's API.
It's rough around the edges but gets the job done!
Expand |
Embed | Plain Text
<?php // The full path to the XML file you exported from Wordpress $xmlFile = ''; // Your tumblr log in details $tumblr_email = ''; $tumblr_password = ''; // Tumblr URL (e.g. http://yourname.tumblr.com) $tumblrUrl = ''; // If a post from Wordpress is a draft, do you want it posted as private so you // have it available? True if so, False to ignore drafts $publishDraftAsPrivate = true; // Full path to a file that is writable, so that a log of current URL on your // wordpress blog to new URL on your tumblr can be written (good for redirects // to preserve links, etc) $logFile = ''; $xml = simplexml_load_file($xmlFile); } else { echo "no such file"; } $nodes = $xml->xpath('/rss/channel/item'); $count = 0; $post_type = 'regular'; $content = $node->children("http://purl.org/rss/1.0/modules/content/"); $post_body = (string)$content->encoded; $publish_status = $node->children("http://wordpress.org/export/1.0/"); $private = 0; if ($publish_status->status != "publish") { if (!$publishDraftAsPrivate) { continue; } $private = 1; } $count++; 'email' => $tumblr_email, 'password' => $tumblr_password, 'type'=> $post_type, 'title'=>$post_title, 'body'=>$post_body, 'generator'=> 'wptumblr-ds', 'private'=>$private ); $request_data = ""; $first = true; foreach ($request as $key=>$value) { if ($first) { $first = false; } else { $request_data .= "&"; } if ($key == "body") { } else { } } $c = curl_init('http://www.tumblr.com/api/write'); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, $request_data); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($c); $status = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_close($c); if ($status == 201) { echo "Success! Post ID: $result"; $res = file_put_contents($logFile,$node->link . " : " . $tumblrUrl . "/post/" . $result,FILE_APPEND); } else if ($status == 403) { echo 'Bad email/password'; } else { echo "Error: $result\n"; } } } ?>
Comments

You need to login to post a comment.
How exactly does the path to the XML file work? I have uploaded it to my server, made it readable to the world, but the script still says no such file.
i keep getting the error: Call to a member function xpath() on a non-object in /home/max/Desktop/wp2tumblr.php on line 27
i imagine this is related to $nodes = $xml->xpath('/rss/channel/item'); but am not experienced enough to solve the problem. any help is greatly appreciated.
i keep getting the error: Call to a member function xpath() on a non-object in /home/max/Desktop/wp2tumblr.php on line 27
i imagine this is related to $nodes = $xml->xpath('/rss/channel/item'); but am not experienced enough to solve the problem. any help is greatly appreciated.
sorry for the double-post. browser wonkiness :-)
Hey! This script has some problems with special characters in the titles of the posts. I have fixed that and added two improvements: Sets the date of the posts in tumblr to the original date of the posts and it does not publish the Wordpress attachments as tumblr posts. I published the modified script at The Source Cookbook: Script to export Wordpress posts to tumblr
In reply to preshit: Even though it states 'full path', it actually works better if you just include all your files in the same folder and DON'T specify the full path. At first I tried "http://blah.com/blahblah.bla" for the XML WordPress file and log file, but that came up with "no such file". Once I just changed it to "blahblah.bla" it worked just fine :)