Are you new? Read the FAQ and catch up on!
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
  1. <?php
  2.  
  3. // The full path to the XML file you exported from Wordpress
  4. $xmlFile = '/full/path/to/wordpress.file.xml';
  5. // Your tumblr log in details
  6. $tumblr_email = 'your@email.com';
  7. $tumblr_password = 'yourpassword';
  8. // Tumblr URL (e.g. http://yourname.tumblr.com)
  9. $tumblrUrl = 'http://name.tumblr.com';
  10. // 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
  11. $publishDraftAsPrivate = true;
  12. // 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)
  13. $logFile = 'log.txt';
  14.  
  15. if (file_exists($xmlFile)) {
  16.  
  17.     $xml = simplexml_load_file($xmlFile);
  18.  
  19. } else {
  20.  
  21.     echo "no such file!!";
  22.  
  23. }
  24.  
  25. if (isset($xml)) {
  26.  
  27.     $nodes = $xml->xpath('/rss/channel/item');
  28.  
  29.     $count = 0;
  30.  
  31.     while(list( , $node) = each($nodes)) {
  32.  
  33.         $post_type = 'regular';
  34.         $post_title = $node->title;
  35.  
  36.         $post_title = str_replace("%20"," ",$post_title);
  37.  
  38.         $content = $node->children("http://purl.org/rss/1.0/modules/content/");            
  39.  
  40.         $post_body = (string)$content->encoded;    
  41.  
  42.         $publish_status = $node->children("http://wordpress.org/export/1.0/");
  43.  
  44.         $date = $publish_status->post_date;
  45.         echo $date;
  46.         $private = 0;
  47.  
  48.         if ($publish_status->status != "publish") {
  49.  
  50.             if (!$publishDraftAsPrivate) {
  51.  
  52.                 continue;
  53.  
  54.             }
  55.  
  56.             $private = 1;
  57.  
  58.         }
  59.  
  60.         if ($publish_status->post_type == "attachment")
  61.             continue;
  62.  
  63.         $count++;
  64.  
  65.         $request = array(
  66.             'email' => $tumblr_email,
  67.             'password' => $tumblr_password,
  68.             'type'=> $post_type,
  69.             'title'=>$post_title,
  70.             'date'=>$date,
  71.             'body'=>$post_body,
  72.             'generator'=> 'wptumblr-ds',
  73.             'private'=>$private
  74.         );
  75.  
  76.         $request_data = "";
  77.  
  78.         $first = true;
  79.         foreach ($request as $key=>$value) {
  80.  
  81.             if ($first) {
  82.                 $first = false;
  83.             } else {
  84.                 $request_data .= "&";          
  85.             }
  86.  
  87.             $request_data .= urlencode($key) . "=";
  88.  
  89.             if ($key == "body") {
  90.  
  91.                 $request_data .= urldecode($value);
  92.  
  93.             } else {
  94.  
  95.                 $request_data .= urlencode($value);
  96.  
  97.             }
  98.  
  99.         }
  100.  
  101.         $c = curl_init('http://www.tumblr.com/api/write');
  102.         curl_setopt($c, CURLOPT_POST, true);
  103.         curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
  104.         curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  105.         $result = curl_exec($c);
  106.         $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  107.         curl_close($c);
  108.  
  109.         if ($status == 201) {
  110.             echo "Success! Post ID: $result";
  111.  
  112.             $res = file_put_contents($logFile,$node->link . " : " . $tumblrUrl . "/post/" . $result,FILE_APPEND);          
  113.         } else if ($status == 403) {
  114.             echo 'Bad email/password';
  115.         } else {
  116.             echo "Error: $result\n";
  117.         }
  118.  
  119.     }  
  120.  
  121. }
  122.  
  123. ?>
  124.  

­

If you find some bug on this code or you have some suggestion, you can email me: miguel.santirso at gmail.com.

Tags: PHP api wordpress tumblr import-export

Embed: