2 require_once ('admin.php');
4 $parent_file = 'edit.php';
6 if ( isset( $_GET['download'] ) )
9 require_once ('admin-header.php');
13 <h2><?php _e('Export'); ?></h2>
15 <p><?php _e('When you click the button below WordPress will create an XML file for you to save to your computer.'); ?></p>
16 <p><?php _e('This format, which we call WordPress eXtended RSS or WXR, will contain your posts, comments, custom fields, and categories.'); ?></p>
17 <p><?php _e('Once you’ve saved the download file, you can use the Import function on another WordPress blog to import this blog.'); ?></p>
18 <form action="" method="get">
19 <h3><?php _e('Optional options'); ?></h3>
23 <th><?php _e('Restrict Author:'); ?></th>
25 <select name="author">
26 <option value="all" selected="selected"><?php _e('All'); ?></option>
28 $authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
29 foreach ( $authors as $id ) {
30 $o = get_userdata( $id );
31 echo "<option value='$o->ID'>$o->display_name</option>";
38 <p class="submit"><input type="submit" name="submit" value="<?php _e('Download Export File'); ?> »" />
39 <input type="hidden" name="download" value="true" />
47 function export_wp() {
48 global $wpdb, $post_ids, $post;
50 do_action('export_wp');
52 $filename = 'wordpress.' . date('Y-m-d') . '.xml';
54 header('Content-Description: File Transfer');
55 header("Content-Disposition: attachment; filename=$filename");
56 header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
59 if ( isset( $_GET['author'] ) && $_GET['author'] != 'all' ) {
60 $author_id = (int) $_GET['author'];
61 $where = " WHERE post_author = '$author_id' ";
64 // grab a snapshot of post IDs, just in case it changes during the export
65 $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
67 $categories = (array) get_categories('get=all');
68 $tags = (array) get_tags('get=all');
70 function wxr_missing_parents($categories) {
71 if ( !is_array($categories) || empty($categories) )
74 foreach ( $categories as $category )
75 $parents[$category->term_id] = $category->parent;
77 $parents = array_unique(array_diff($parents, array_keys($parents)));
79 if ( $zero = array_search('0', $parents) )
80 unset($parents[$zero]);
85 while ( $parents = wxr_missing_parents($categories) ) {
86 $found_parents = get_categories("include=" . join(', ', $parents));
87 if ( is_array($found_parents) && count($found_parents) )
88 $categories = array_merge($categories, $found_parents);
93 // Put them in order to be inserted with no child going before its parent
95 $passes = 1000 + count($categories);
96 while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
97 if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
98 $cats[$cat->term_id] = $cat;
100 $categories[] = $cat;
105 function wxr_cdata($str) {
106 if ( seems_utf8($str) == false )
107 $str = utf8_encode($str);
109 // $str = ent2ncr(wp_specialchars($str));
111 $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
116 function wxr_cat_name($c) {
117 if ( empty($c->name) )
120 echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
123 function wxr_category_description($c) {
124 if ( empty($c->description) )
127 echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
130 function wxr_tag_name($t) {
131 if ( empty($t->name) )
134 echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
137 function wxr_tag_description($t) {
138 if ( empty($t->description) )
141 echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
144 function wxr_post_taxonomy() {
145 $categories = get_the_category();
146 $tags = get_the_tags();
147 $cat_names = array();
148 $tag_names = array();
152 if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
153 $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
154 $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
157 if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
158 $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
159 $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
165 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
168 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
169 <!-- It contains information about your blog's posts, comments, and categories. -->
170 <!-- You may use this file to transfer that content from one site to another. -->
171 <!-- This file is not intended to serve as a complete backup of your blog. -->
173 <!-- To import this information into a WordPress blog follow these steps. -->
174 <!-- 1. Log into that blog as an administrator. -->
175 <!-- 2. Go to Manage: Import in the blog's admin panels. -->
176 <!-- 3. Choose "WordPress" from the list. -->
177 <!-- 4. Upload this file using the form provided on that page. -->
178 <!-- 5. You will first be asked to map the authors in this export file to users -->
179 <!-- on the blog. For each author, you may choose to map to an -->
180 <!-- existing user on the blog or to create a new user -->
181 <!-- 6. WordPress will then import each of the posts, comments, and categories -->
182 <!-- contained in this file into your blog -->
184 <!-- generator="wordpress/<?php bloginfo_rss('version') ?>" created="<?php echo date('Y-m-d H:i'); ?>"-->
186 xmlns:content="http://purl.org/rss/1.0/modules/content/"
187 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
188 xmlns:dc="http://purl.org/dc/elements/1.1/"
189 xmlns:wp="http://wordpress.org/export/1.0/"
193 <title><?php bloginfo_rss('name'); ?></title>
194 <link><?php bloginfo_rss('url') ?></link>
195 <description><?php bloginfo_rss("description") ?></description>
196 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
197 <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
198 <language><?php echo get_option('rss_language'); ?></language>
199 <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
200 <wp:category><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->name : ''; ?></wp:category_parent><?php wxr_cat_name($c); ?><?php wxr_category_description($c); ?></wp:category>
201 <?php endforeach; endif; ?>
202 <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
203 <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
204 <?php endforeach; endif; ?>
205 <?php do_action('rss2_head'); ?>
206 <?php if ($post_ids) {
208 $wp_query->in_the_loop = true; // Fake being in the loop.
209 // fetch 20 posts at a time rather than loading the entire table into memory
210 while ( $next_posts = array_splice($post_ids, 0, 20) ) {
211 $where = "WHERE ID IN (".join(',', $next_posts).")";
212 $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC");
213 foreach ($posts as $post) {
214 setup_postdata($post); ?>
216 <title><?php the_title_rss() ?></title>
217 <link><?php the_permalink_rss() ?></link>
218 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
219 <dc:creator><?php the_author() ?></dc:creator>
220 <?php wxr_post_taxonomy() ?>
222 <guid isPermaLink="false"><?php the_guid(); ?></guid>
223 <description></description>
224 <content:encoded><![CDATA[<?php echo $post->post_content ?>]]></content:encoded>
225 <wp:post_id><?php echo $post->ID; ?></wp:post_id>
226 <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
227 <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
228 <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
229 <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
230 <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
231 <wp:status><?php echo $post->post_status; ?></wp:status>
232 <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
233 <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
234 <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
236 $postmeta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID");
239 <?php foreach( $postmeta as $meta ) { ?>
241 <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
242 <wp:meta_value><?Php echo $meta->meta_value; ?></wp:meta_value>
247 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID");
248 if ( $comments ) { foreach ( $comments as $c ) { ?>
250 <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
251 <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
252 <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
253 <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
254 <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
255 <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
256 <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
257 <wp:comment_content><?php echo $c->comment_content; ?></wp:comment_content>
258 <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
259 <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
260 <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
271 include ('admin-footer.php');