<?php //Export a wordpress blog as an XML file. // Edit these to suit your setup. DEFINE ('DB_USER', 'user_name'); DEFINE ('DB_PASSWORD', 'password'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'wordpress'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db(DB_NAME)) { die("Could not connect to DB"); } $query = "SELECT ID, post_date, post_title, post_status, post_modified, post_content FROM wp1_posts ORDER BY ID ASC"; $result = @mysql_query ($query); if ($result) { //Path and name of the output file. //This should be somewhere writeable by the web server. $handle = fopen('/your_path/wordpress_export.xml', 'w'); if (! $handle) { die("Could not open file."); } fwrite($handle, "<?xml version=\"1.0\"?>\n"); fwrite($handle, "<blog>\n"); // Iterate through all records while ($row = mysql_fetch_array($result, MYSQL_NUM)) { fwrite($handle, "<entry>\n"); fwrite($handle, "<POSTID>" . $row[0] . "</POSTID>\n"); fwrite($handle, "<POSTDATE>" . $row[1] . "</POSTDATE>\n"); fwrite($handle, "<POSTTITLE>" . $row[2] . "</POSTTITLE>\n"); fwrite($handle, "<POSTSTATUS>" . $row[3] . "</POSTSTATUS>\n"); fwrite($handle, "<POSTMODIFIED>" . $row[4] . "</POSTMODIFIED>\n"); fwrite($handle, "<BODY>"); $temp = $row[5]; // Escape ampersands that aren't part of entities. $temp = preg_replace('/&(?!\w{2,6};)/', '&', $temp); $temp = preg_replace('/<(?!\w{2,6};)/', '<', $temp); $temp = preg_replace('/<(?!\w{2,6};)/', '&rt;', $temp); //replace newline characters with %0a $temp = preg_replace('/\n+/s', '%0a', $temp); $temp = preg_replace('/\r+/s', '', $temp); fwrite($handle, $temp); fwrite($handle, "</BODY>\n"); fwrite($handle, "</entry>\n\n"); } fwrite($handle, "</blog>\n"); fclose($handle); } else { echo "<p> Error executing query. </p>"; } mysql_close($link); ?>