1k views
Script to export Wordpress posts to Tumblr
Tumblr does not provide a way to import posts from Wordpress. To solve that, I fixed some code I found on the internet and it seems to work. Here it is:
PHP
- <?php
- // The full path to the XML file you exported from Wordpress
- $xmlFile = '/full/path/to/wordpress.file.xml';
- // Your tumblr log in details
- $tumblr_email = 'your@email.com';
- $tumblr_password = 'yourpassword';
- // Tumblr URL (e.g. http://yourname.tumblr.com)
- $tumblrUrl = 'http://name.tumblr.com';
- // 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 = 'log.txt';
- } else {
- echo "no such file!!";
- }
- $nodes = $xml->xpath('/rss/channel/item');
- $count = 0;
- $post_type = 'regular';
- $post_title = $node->title;
- $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/");
- $date = $publish_status->post_date;
- echo $date;
- $private = 0;
- if ($publish_status->status != "publish") {
- if (!$publishDraftAsPrivate) {
- continue;
- }
- $private = 1;
- }
- if ($publish_status->post_type == "attachment")
- continue;
- $count++;
- 'email' => $tumblr_email,
- 'password' => $tumblr_password,
- 'type'=> $post_type,
- 'title'=>$post_title,
- 'date'=>$date,
- '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 {
- }
- }
- 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";
- }
- }
- }
- ?>
If you find some bug on this code or you have some suggestion, you can email me: miguel.santirso at gmail.com.
Submitted by miguelSantirso 5 months ago — last modified less than a minute ago