Migrate Wordpress to Tumblr


/ Published in: PHP
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // The full path to the XML file you exported from Wordpress
  4. $xmlFile = '';
  5. // Your tumblr log in details
  6. $tumblr_email = '';
  7. $tumblr_password = '';
  8. // Tumblr URL (e.g. http://yourname.tumblr.com)
  9. $tumblrUrl = '';
  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 = '';
  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 = rawurlencode($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. $private = 0;
  45.  
  46. if ($publish_status->status != "publish") {
  47.  
  48. if (!$publishDraftAsPrivate) {
  49.  
  50. continue;
  51.  
  52. }
  53.  
  54. $private = 1;
  55.  
  56. }
  57.  
  58. $count++;
  59.  
  60. $request = array(
  61. 'email' => $tumblr_email,
  62. 'password' => $tumblr_password,
  63. 'type'=> $post_type,
  64. 'title'=>$post_title,
  65. 'body'=>$post_body,
  66. 'generator'=> 'wptumblr-ds',
  67. 'private'=>$private
  68. );
  69.  
  70. $request_data = "";
  71.  
  72. $first = true;
  73. foreach ($request as $key=>$value) {
  74.  
  75. if ($first) {
  76. $first = false;
  77. } else {
  78. $request_data .= "&";
  79. }
  80.  
  81. $request_data .= urlencode($key) . "=";
  82.  
  83. if ($key == "body") {
  84.  
  85. $request_data .= urldecode($value);
  86.  
  87. } else {
  88.  
  89. $request_data .= urlencode($value);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. $c = curl_init('http://www.tumblr.com/api/write');
  96. curl_setopt($c, CURLOPT_POST, true);
  97. curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
  98. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  99. $result = curl_exec($c);
  100. $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  101.  
  102. if ($status == 201) {
  103. echo "Success! Post ID: $result";
  104.  
  105. $res = file_put_contents($logFile,$node->link . " : " . $tumblrUrl . "/post/" . $result,FILE_APPEND);
  106. } else if ($status == 403) {
  107. echo 'Bad email/password';
  108. } else {
  109. echo "Error: $result\n";
  110. }
  111.  
  112. }
  113.  
  114. }
  115.  
  116. ?>

URL: http://shakefon.tumblr.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.