3 * WordPress Export Administration API
6 * @subpackage Administration
10 * Version number for the export format.
12 * Bump this when something changes that might affect compatibility.
17 define('WXR_VERSION', '1.0');
20 * {@internal Missing Short Description}}
24 * @param unknown_type $args
26 function export_wp( $args = array() ) {
27 global $wpdb, $post_ids, $post, $wp_taxonomies;
29 if ( ! is_array( $args ) )
30 $args = array( 'author' => $args );
32 $defaults = array( 'author' => null, 'taxonomy' => null, 'post_type' => null, 'post_status' => null, 'start_date' => null, 'end_date' => null );
33 $args = wp_parse_args( $args, $defaults );
37 do_action('export_wp');
39 if( strlen( $start_date ) > 4 && strlen( $end_date ) > 4 )
40 $filename = 'wordpress.' . $start_date . '.' . $end_date . '.xml';
42 $filename = 'wordpress.' . date( 'Y-m-d' ) . '.xml';
44 header( 'Content-Description: File Transfer' );
45 header( 'Content-Disposition: attachment; filename=' . $filename );
46 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
48 if ( $post_type && $post_type != 'all' )
49 $where = $wpdb->prepare("WHERE post_type = %s ", $post_type);
51 $where = "WHERE post_type != 'revision' ";
53 if ( $author && $author != 'all' ) {
54 $author_id = (int) $author;
55 $where .= $wpdb->prepare( "AND post_author = %d ", $author_id );
58 if ( $start_date && $start_date != 'all' )
59 $where .= $wpdb->prepare( "AND post_date >= %s ", $start_date );
61 if ( $end_date && $end_date != 'all' )
62 $where .= $wpdb->prepare( "AND post_date < %s ", $end_date );
64 if ( $taxonomy && is_array( $taxonomy ) ) {
65 foreach ( $taxonomy as $term_id ) {
66 if ( $term_id != 'all' )
67 $where .= $wpdb->prepare( "AND ID IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d) ", $term_id );
71 if ( $post_status && $post_status != 'all' )
72 $where .= $wpdb->prepare( "AND post_status = %s", $post_status );
74 // grab a snapshot of post IDs, just in case it changes during the export
75 $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
77 $categories = (array) get_categories( array( 'get' => 'all' ) );
78 $tags = (array) get_tags( array( 'get' => 'all' ) );
80 $custom_taxonomies = $wp_taxonomies;
81 unset( $custom_taxonomies['category'] );
82 unset( $custom_taxonomies['post_tag'] );
83 unset( $custom_taxonomies['link_category'] );
84 $custom_taxonomies = array_keys( $custom_taxonomies );
85 $terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
88 * {@internal Missing Short Description}}
92 * @param unknown_type $categories
94 function wxr_missing_parents( $categories ) {
95 if ( ! is_array( $categories ) || empty( $categories ) )
98 foreach ( $categories as $category ){
99 $parents[$category->term_id] = $category->parent;
102 $parents = array_unique( array_diff( $parents, array_keys( $parents ) ) );
104 if ( $zero = array_search( '0', $parents ) )
105 unset( $parents[$zero] );
110 while ( $parents = wxr_missing_parents( $categories ) ) {
111 $found_parents = get_categories( array( 'include' => join( ', ', $parents) ) );
112 if ( is_array( $found_parents ) && count( $found_parents ) )
113 $categories = array_merge( $categories, $found_parents );
118 // Put them in order to be inserted with no child going before its parent
120 $passes = 1000 + count( $categories );
121 while ( ( $cat = array_shift( $categories ) ) && ++$pass < $passes ) {
122 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
123 $cats[$cat->term_id] = $cat;
125 $categories[] = $cat;
127 unset( $categories );
130 * Place string in CDATA tag.
134 * @param string $str String to place in XML CDATA tag.
136 function wxr_cdata( $str ) {
137 if ( seems_utf8( $str ) == false )
138 $str = utf8_encode( $str );
140 // $str = ent2ncr(esc_html($str));
141 $str = "<![CDATA[$str" . ( ( substr( $str, -1 ) == ']' ) ? ' ' : '') . "]]>";
147 * {@internal Missing Short Description}}
151 * @return string Site URL.
153 function wxr_site_url() {
154 global $current_site;
157 if ( isset( $current_site->domain ) )
158 return network_home_url();
161 return get_bloginfo_rss( 'url' );
165 * {@internal Missing Short Description}}
169 * @param object $c Category Object
171 function wxr_cat_name( $c ) {
172 if ( empty( $c->name ) )
175 echo '<wp:cat_name>' . wxr_cdata( $c->name ) . '</wp:cat_name>';
179 * {@internal Missing Short Description}}
183 * @param object $c Category Object
185 function wxr_category_description( $c ) {
186 if ( empty( $c->description ) )
189 echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
193 * {@internal Missing Short Description}}
197 * @param object $t Tag Object
199 function wxr_tag_name( $t ) {
200 if ( empty( $t->name ) )
203 echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
207 * {@internal Missing Short Description}}
211 * @param object $t Tag Object
213 function wxr_tag_description( $t ) {
214 if ( empty( $t->description ) )
217 echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
221 * {@internal Missing Short Description}}
225 * @param object $t Term Object
227 function wxr_term_name( $t ) {
228 if ( empty( $t->name ) )
231 echo '<wp:term_name>' . wxr_cdata($t->name) . '</wp:term_name>';
235 * {@internal Missing Short Description}}
239 * @param object $t Term Object
241 function wxr_term_description( $t ) {
242 if ( empty( $t->description ) )
245 echo '<wp:term_description>' . wxr_cdata($t->description) . '</wp:term_description>';
249 * {@internal Missing Short Description}}
253 function wxr_post_taxonomy() {
259 $taxonomies = get_object_taxonomies( 'post' );
260 $terms = wp_get_post_terms( $post->ID, $taxonomies );
261 foreach ( (array) $terms as $term ) {
262 $domain = ( 'post_tag' == $term->taxonomy ) ? 'tag' : $term->taxonomy;
263 $term_name = sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, $filter );
265 if ( 'category' == $term->taxonomy )
266 $the_list .= "\n\t\t<category><![CDATA[$term_name]]></category>\n";
267 elseif ( 'post_tag' == $term->taxonomy )
268 $the_list .= "\n\t\t<category domain=\"$domain\"><![CDATA[$term_name]]></category>\n";
269 // forwards compatibility as above
270 $the_list .= "\n\t\t<category domain=\"$domain\" nicename=\"{$term->slug}\"><![CDATA[$term_name]]></category>\n";
275 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
278 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
279 <!-- It contains information about your blog's posts, comments, and categories. -->
280 <!-- You may use this file to transfer that content from one site to another. -->
281 <!-- This file is not intended to serve as a complete backup of your blog. -->
283 <!-- To import this information into a WordPress blog follow these steps. -->
284 <!-- 1. Log in to that blog as an administrator. -->
285 <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
286 <!-- 3. Choose "WordPress" from the list. -->
287 <!-- 4. Upload this file using the form provided on that page. -->
288 <!-- 5. You will first be asked to map the authors in this export file to users -->
289 <!-- on the blog. For each author, you may choose to map to an -->
290 <!-- existing user on the blog or to create a new user -->
291 <!-- 6. WordPress will then import each of the posts, comments, and categories -->
292 <!-- contained in this file into your blog -->
294 <?php the_generator( 'export' );?>
296 xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
297 xmlns:content="http://purl.org/rss/1.0/modules/content/"
298 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
299 xmlns:dc="http://purl.org/dc/elements/1.1/"
300 xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
304 <title><?php bloginfo_rss( 'name' ); ?></title>
305 <link><?php bloginfo_rss( 'url' ) ?></link>
306 <description><?php bloginfo_rss( 'description' ) ?></description>
307 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></pubDate>
308 <generator>http://wordpress.org/?v=<?php bloginfo_rss( 'version' ); ?></generator>
309 <language><?php echo get_option( 'rss_language' ); ?></language>
310 <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
311 <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
312 <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
313 <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
314 <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>
315 <?php endforeach; endif; ?>
316 <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
317 <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name( $t ); ?><?php wxr_tag_description( $t ); ?></wp:tag>
318 <?php endforeach; endif; ?>
319 <?php if ( $terms ) : foreach ( $terms as $t ) : ?>
320 <wp:term><wp:term_taxonomy><?php echo $t->taxonomy; ?></wp:term_taxonomy><wp:term_slug><?php echo $t->slug; ?></wp:term_slug><wp:term_parent><?php echo $t->parent ? $custom_taxonomies[$t->parent]->name : ''; ?></wp:term_parent><?php wxr_term_name( $t ); ?><?php wxr_term_description( $t ); ?></wp:term>
321 <?php endforeach; endif; ?>
323 <?php do_action( 'rss2_head' ); ?>
325 <?php if ( $post_ids ) {
327 $wp_query->in_the_loop = true; // Fake being in the loop.
329 // fetch 20 posts at a time rather than loading the entire table into memory
330 while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
331 $where = "WHERE ID IN (" . join( ',', $next_posts ) . ")";
332 $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts $where ORDER BY post_date_gmt ASC" );
335 foreach ($posts as $post) {
336 setup_postdata( $post );
339 if ( is_sticky( $post->ID ) )
344 <title><?php echo apply_filters( 'the_title_rss', $post->post_title ); ?></title>
345 <link><?php the_permalink_rss() ?></link>
346 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
347 <dc:creator><?php echo wxr_cdata( get_the_author() ); ?></dc:creator>
348 <?php wxr_post_taxonomy() ?>
350 <guid isPermaLink="false"><?php esc_url( the_guid() ); ?></guid>
351 <description></description>
352 <content:encoded><?php echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) ); ?></content:encoded>
353 <excerpt:encoded><?php echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) ); ?></excerpt:encoded>
354 <wp:post_id><?php echo $post->ID; ?></wp:post_id>
355 <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
356 <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
357 <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
358 <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
359 <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
360 <wp:status><?php echo $post->post_status; ?></wp:status>
361 <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
362 <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
363 <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
364 <wp:post_password><?php echo $post->post_password; ?></wp:post_password>
365 <wp:is_sticky><?php echo $is_sticky; ?></wp:is_sticky>
367 if ( $post->post_type == 'attachment' ) { ?>
368 <wp:attachment_url><?php echo wp_get_attachment_url( $post->ID ); ?></wp:attachment_url>
371 $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
374 <?php foreach( $postmeta as $meta ) { ?>
376 <wp:meta_key><?php echo $meta->meta_key; ?></wp:meta_key>
377 <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
382 $comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID ) );
383 if ( $comments ) { foreach ( $comments as $c ) { ?>
385 <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
386 <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
387 <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
388 <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
389 <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
390 <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
391 <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
392 <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
393 <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
394 <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
395 <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
396 <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>