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.
16 define( 'WXR_VERSION', '1.2' );
19 * Generates the WXR export file for download.
21 * Default behavior is to export all content, however, note that post content will only
22 * be exported for post types with the `can_export` argument enabled. Any posts with the
23 * 'auto-draft' status will be skipped.
27 * @global wpdb $wpdb WordPress database abstraction object.
28 * @global WP_Post $post Global `$post`.
30 * @param array $args {
31 * Optional. Arguments for generating the WXR export file for download. Default empty array.
33 * @type string $content Type of content to export. If set, only the post content of this post type
34 * will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined
35 * custom post. If an invalid custom post type is supplied, every post type for
36 * which `can_export` is enabled will be exported instead. If a valid custom post
37 * type is supplied but `can_export` is disabled, then 'posts' will be exported
38 * instead. When 'all' is supplied, only post types with `can_export` enabled will
39 * be exported. Default 'all'.
40 * @type string $author Author to export content for. Only used when `$content` is 'post', 'page', or
41 * 'attachment'. Accepts false (all) or a specific author ID. Default false (all).
42 * @type string $category Category (slug) to export content for. Used only when `$content` is 'post'. If
43 * set, only post content assigned to `$category will be exported. Accepts false
44 * or a specific category slug. Default is false (all categories).
45 * @type string $start_date Start date to export content from. Expected date format is 'Y-m-d'. Used only
46 * when `$content` is 'post', 'page' or 'attachment'. Default false (since the
48 * @type string $end_date End date to export content to. Expected date format is 'Y-m-d'. Used only when
49 * `$content` is 'post', 'page' or 'attachment'. Default false (latest publish date).
50 * @type string $status Post status to export posts for. Used only when `$content` is 'post' or 'page'.
51 * Accepts false (all statuses except 'auto-draft'), or a specific status, i.e.
52 * 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', or
53 * 'trash'. Default false (all statuses except 'auto-draft').
56 function export_wp( $args = array() ) {
59 $defaults = array( 'content' => 'all', 'author' => false, 'category' => false,
60 'start_date' => false, 'end_date' => false, 'status' => false,
62 $args = wp_parse_args( $args, $defaults );
65 * Fires at the beginning of an export, before any headers are sent.
69 * @param array $args An array of export arguments.
71 do_action( 'export_wp', $args );
73 $sitename = sanitize_key( get_bloginfo( 'name' ) );
74 if ( ! empty( $sitename ) ) {
77 $date = date( 'Y-m-d' );
78 $wp_filename = $sitename . 'wordpress.' . $date . '.xml';
80 * Filters the export filename.
84 * @param string $wp_filename The name of the file for download.
85 * @param string $sitename The site name.
86 * @param string $date Today's date, formatted.
88 $filename = apply_filters( 'export_wp_filename', $wp_filename, $sitename, $date );
90 header( 'Content-Description: File Transfer' );
91 header( 'Content-Disposition: attachment; filename=' . $filename );
92 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
94 if ( 'all' != $args['content'] && post_type_exists( $args['content'] ) ) {
95 $ptype = get_post_type_object( $args['content'] );
96 if ( ! $ptype->can_export )
97 $args['content'] = 'post';
99 $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
101 $post_types = get_post_types( array( 'can_export' => true ) );
102 $esses = array_fill( 0, count($post_types), '%s' );
103 $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
106 if ( $args['status'] && ( 'post' == $args['content'] || 'page' == $args['content'] ) )
107 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
109 $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
112 if ( $args['category'] && 'post' == $args['content'] ) {
113 if ( $term = term_exists( $args['category'], 'category' ) ) {
114 $join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
115 $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
119 if ( 'post' == $args['content'] || 'page' == $args['content'] || 'attachment' == $args['content'] ) {
120 if ( $args['author'] )
121 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
123 if ( $args['start_date'] )
124 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", date( 'Y-m-d', strtotime($args['start_date']) ) );
126 if ( $args['end_date'] )
127 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", date( 'Y-m-d', strtotime('+1 month', strtotime($args['end_date'])) ) );
130 // Grab a snapshot of post IDs, just in case it changes during the export.
131 $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );
134 * Get the requested terms ready, empty unless posts filtered by category
137 $cats = $tags = $terms = array();
138 if ( isset( $term ) && $term ) {
139 $cat = get_term( $term['term_id'], 'category' );
140 $cats = array( $cat->term_id => $cat );
141 unset( $term, $cat );
142 } elseif ( 'all' == $args['content'] ) {
143 $categories = (array) get_categories( array( 'get' => 'all' ) );
144 $tags = (array) get_tags( array( 'get' => 'all' ) );
146 $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
147 $custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
149 // Put categories in order with no child going before its parent.
150 while ( $cat = array_shift( $categories ) ) {
151 if ( $cat->parent == 0 || isset( $cats[$cat->parent] ) )
152 $cats[$cat->term_id] = $cat;
154 $categories[] = $cat;
157 // Put terms in order with no child going before its parent.
158 while ( $t = array_shift( $custom_terms ) ) {
159 if ( $t->parent == 0 || isset( $terms[$t->parent] ) )
160 $terms[$t->term_id] = $t;
162 $custom_terms[] = $t;
165 unset( $categories, $custom_taxonomies, $custom_terms );
169 * Wrap given string in XML CDATA tag.
173 * @param string $str String to wrap in XML CDATA tag.
176 function wxr_cdata( $str ) {
177 if ( ! seems_utf8( $str ) ) {
178 $str = utf8_encode( $str );
180 // $str = ent2ncr(esc_html($str));
181 $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
187 * Return the URL of the site
191 * @return string Site URL.
193 function wxr_site_url() {
194 // Multisite: the base URL.
195 if ( is_multisite() )
196 return network_home_url();
197 // WordPress (single site): the blog URL.
199 return get_bloginfo_rss( 'url' );
203 * Output a cat_name XML tag from a given category object
207 * @param object $category Category Object
209 function wxr_cat_name( $category ) {
210 if ( empty( $category->name ) )
213 echo '<wp:cat_name>' . wxr_cdata( $category->name ) . "</wp:cat_name>\n";
217 * Output a category_description XML tag from a given category object
221 * @param object $category Category Object
223 function wxr_category_description( $category ) {
224 if ( empty( $category->description ) )
227 echo '<wp:category_description>' . wxr_cdata( $category->description ) . "</wp:category_description>\n";
231 * Output a tag_name XML tag from a given tag object
235 * @param object $tag Tag Object
237 function wxr_tag_name( $tag ) {
238 if ( empty( $tag->name ) )
241 echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . "</wp:tag_name>\n";
245 * Output a tag_description XML tag from a given tag object
249 * @param object $tag Tag Object
251 function wxr_tag_description( $tag ) {
252 if ( empty( $tag->description ) )
255 echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . "</wp:tag_description>\n";
259 * Output a term_name XML tag from a given term object
263 * @param object $term Term Object
265 function wxr_term_name( $term ) {
266 if ( empty( $term->name ) )
269 echo '<wp:term_name>' . wxr_cdata( $term->name ) . "</wp:term_name>\n";
273 * Output a term_description XML tag from a given term object
277 * @param object $term Term Object
279 function wxr_term_description( $term ) {
280 if ( empty( $term->description ) )
283 echo "\t\t<wp:term_description>" . wxr_cdata( $term->description ) . "</wp:term_description>\n";
287 * Output term meta XML tags for a given term object.
291 * @param WP_Term $term Term object.
293 function wxr_term_meta( $term ) {
296 $termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) );
298 foreach ( $termmeta as $meta ) {
300 * Filters whether to selectively skip term meta used for WXR exports.
302 * Returning a truthy value to the filter will skip the current meta
303 * object from being exported.
307 * @param bool $skip Whether to skip the current piece of term meta. Default false.
308 * @param string $meta_key Current meta key.
309 * @param object $meta Current meta object.
311 if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) {
312 printf( "\t\t<wp:termmeta>\n\t\t\t<wp:meta_key>%s</wp:meta_key>\n\t\t\t<wp:meta_value>%s</wp:meta_value>\n\t\t</wp:termmeta>\n", wxr_cdata( $meta->meta_key ), wxr_cdata( $meta->meta_value ) );
318 * Output list of authors with posts
322 * @global wpdb $wpdb WordPress database abstraction object.
324 * @param array $post_ids Array of post IDs to filter the query by. Optional.
326 function wxr_authors_list( array $post_ids = null ) {
329 if ( !empty( $post_ids ) ) {
330 $post_ids = array_map( 'absint', $post_ids );
331 $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
337 $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" );
338 foreach ( (array) $results as $result )
339 $authors[] = get_userdata( $result->post_author );
341 $authors = array_filter( $authors );
343 foreach ( $authors as $author ) {
344 echo "\t<wp:author>";
345 echo '<wp:author_id>' . intval( $author->ID ) . '</wp:author_id>';
346 echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>';
347 echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>';
348 echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>';
349 echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>';
350 echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>';
351 echo "</wp:author>\n";
356 * Output all navigation menu terms
360 function wxr_nav_menu_terms() {
361 $nav_menus = wp_get_nav_menus();
362 if ( empty( $nav_menus ) || ! is_array( $nav_menus ) )
365 foreach ( $nav_menus as $menu ) {
367 echo '<wp:term_id>' . intval( $menu->term_id ) . '</wp:term_id>';
368 echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>';
369 echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>';
370 wxr_term_name( $menu );
376 * Output list of taxonomy terms, in XML tag format, associated with a post
380 function wxr_post_taxonomy() {
383 $taxonomies = get_object_taxonomies( $post->post_type );
384 if ( empty( $taxonomies ) )
386 $terms = wp_get_object_terms( $post->ID, $taxonomies );
388 foreach ( (array) $terms as $term ) {
389 echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n";
395 * @param bool $return_me
396 * @param string $meta_key
399 function wxr_filter_postmeta( $return_me, $meta_key ) {
400 if ( '_edit_lock' == $meta_key )
404 add_filter( 'wxr_export_skip_postmeta', 'wxr_filter_postmeta', 10, 2 );
406 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
409 <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
410 <!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
411 <!-- You may use this file to transfer that content from one site to another. -->
412 <!-- This file is not intended to serve as a complete backup of your site. -->
414 <!-- To import this information into a WordPress site follow these steps: -->
415 <!-- 1. Log in to that site as an administrator. -->
416 <!-- 2. Go to Tools: Import in the WordPress admin panel. -->
417 <!-- 3. Install the "WordPress" importer from the list. -->
418 <!-- 4. Activate & Run Importer. -->
419 <!-- 5. Upload this file using the form provided on that page. -->
420 <!-- 6. You will first be asked to map the authors in this export file to users -->
421 <!-- on the site. For each author, you may choose to map to an -->
422 <!-- existing user on the site or to create a new user. -->
423 <!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
424 <!-- contained in this file into your site. -->
426 <?php the_generator( 'export' ); ?>
428 xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
429 xmlns:content="http://purl.org/rss/1.0/modules/content/"
430 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
431 xmlns:dc="http://purl.org/dc/elements/1.1/"
432 xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
436 <title><?php bloginfo_rss( 'name' ); ?></title>
437 <link><?php bloginfo_rss( 'url' ); ?></link>
438 <description><?php bloginfo_rss( 'description' ); ?></description>
439 <pubDate><?php echo date( 'D, d M Y H:i:s +0000' ); ?></pubDate>
440 <language><?php bloginfo_rss( 'language' ); ?></language>
441 <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
442 <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
443 <wp:base_blog_url><?php bloginfo_rss( 'url' ); ?></wp:base_blog_url>
445 <?php wxr_authors_list( $post_ids ); ?>
447 <?php foreach ( $cats as $c ) : ?>
449 <wp:term_id><?php echo intval( $c->term_id ); ?></wp:term_id>
450 <wp:category_nicename><?php echo wxr_cdata( $c->slug ); ?></wp:category_nicename>
451 <wp:category_parent><?php echo wxr_cdata( $c->parent ? $cats[$c->parent]->slug : '' ); ?></wp:category_parent>
452 <?php wxr_cat_name( $c );
453 wxr_category_description( $c );
454 wxr_term_meta( $c ); ?>
457 <?php foreach ( $tags as $t ) : ?>
459 <wp:term_id><?php echo intval( $t->term_id ); ?></wp:term_id>
460 <wp:tag_slug><?php echo wxr_cdata( $t->slug ); ?></wp:tag_slug>
461 <?php wxr_tag_name( $t );
462 wxr_tag_description( $t );
463 wxr_term_meta( $t ); ?>
466 <?php foreach ( $terms as $t ) : ?>
468 <wp:term_id><?php echo wxr_cdata( $t->term_id ); ?></wp:term_id>
469 <wp:term_taxonomy><?php echo wxr_cdata( $t->taxonomy ); ?></wp:term_taxonomy>
470 <wp:term_slug><?php echo wxr_cdata( $t->slug ); ?></wp:term_slug>
471 <wp:term_parent><?php echo wxr_cdata( $t->parent ? $terms[$t->parent]->slug : '' ); ?></wp:term_parent>
472 <?php wxr_term_name( $t );
473 wxr_term_description( $t );
474 wxr_term_meta( $t ); ?>
477 <?php if ( 'all' == $args['content'] ) wxr_nav_menu_terms(); ?>
480 /** This action is documented in wp-includes/feed-rss2.php */
481 do_action( 'rss2_head' );
484 <?php if ( $post_ids ) {
486 * @global WP_Query $wp_query
490 // Fake being in the loop.
491 $wp_query->in_the_loop = true;
493 // Fetch 20 posts at a time rather than loading the entire table into memory.
494 while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
495 $where = 'WHERE ID IN (' . join( ',', $next_posts ) . ')';
496 $posts = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} $where" );
499 foreach ( $posts as $post ) {
500 setup_postdata( $post );
501 $is_sticky = is_sticky( $post->ID ) ? 1 : 0;
505 /** This filter is documented in wp-includes/feed.php */
506 echo apply_filters( 'the_title_rss', $post->post_title );
508 <link><?php the_permalink_rss() ?></link>
509 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
510 <dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
511 <guid isPermaLink="false"><?php the_guid(); ?></guid>
512 <description></description>
513 <content:encoded><?php
515 * Filters the post content used for WXR exports.
519 * @param string $post_content Content of the current post.
521 echo wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) );
523 <excerpt:encoded><?php
525 * Filters the post excerpt used for WXR exports.
529 * @param string $post_excerpt Excerpt for the current post.
531 echo wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) );
533 <wp:post_id><?php echo intval( $post->ID ); ?></wp:post_id>
534 <wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
535 <wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
536 <wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
537 <wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
538 <wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
539 <wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
540 <wp:post_parent><?php echo intval( $post->post_parent ); ?></wp:post_parent>
541 <wp:menu_order><?php echo intval( $post->menu_order ); ?></wp:menu_order>
542 <wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
543 <wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
544 <wp:is_sticky><?php echo intval( $is_sticky ); ?></wp:is_sticky>
545 <?php if ( $post->post_type == 'attachment' ) : ?>
546 <wp:attachment_url><?php echo wxr_cdata( wp_get_attachment_url( $post->ID ) ); ?></wp:attachment_url>
548 <?php wxr_post_taxonomy(); ?>
549 <?php $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
550 foreach ( $postmeta as $meta ) :
552 * Filters whether to selectively skip post meta used for WXR exports.
554 * Returning a truthy value to the filter will skip the current meta
555 * object from being exported.
559 * @param bool $skip Whether to skip the current post meta. Default false.
560 * @param string $meta_key Current meta key.
561 * @param object $meta Current meta object.
563 if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) )
567 <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
568 <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>
572 $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
573 $comments = array_map( 'get_comment', $_comments );
574 foreach ( $comments as $c ) : ?>
576 <wp:comment_id><?php echo intval( $c->comment_ID ); ?></wp:comment_id>
577 <wp:comment_author><?php echo wxr_cdata( $c->comment_author ); ?></wp:comment_author>
578 <wp:comment_author_email><?php echo wxr_cdata( $c->comment_author_email ); ?></wp:comment_author_email>
579 <wp:comment_author_url><?php echo esc_url_raw( $c->comment_author_url ); ?></wp:comment_author_url>
580 <wp:comment_author_IP><?php echo wxr_cdata( $c->comment_author_IP ); ?></wp:comment_author_IP>
581 <wp:comment_date><?php echo wxr_cdata( $c->comment_date ); ?></wp:comment_date>
582 <wp:comment_date_gmt><?php echo wxr_cdata( $c->comment_date_gmt ); ?></wp:comment_date_gmt>
583 <wp:comment_content><?php echo wxr_cdata( $c->comment_content ) ?></wp:comment_content>
584 <wp:comment_approved><?php echo wxr_cdata( $c->comment_approved ); ?></wp:comment_approved>
585 <wp:comment_type><?php echo wxr_cdata( $c->comment_type ); ?></wp:comment_type>
586 <wp:comment_parent><?php echo intval( $c->comment_parent ); ?></wp:comment_parent>
587 <wp:comment_user_id><?php echo intval( $c->user_id ); ?></wp:comment_user_id>
588 <?php $c_meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $c->comment_ID ) );
589 foreach ( $c_meta as $meta ) :
591 * Filters whether to selectively skip comment meta used for WXR exports.
593 * Returning a truthy value to the filter will skip the current meta
594 * object from being exported.
598 * @param bool $skip Whether to skip the current comment meta. Default false.
599 * @param string $meta_key Current meta key.
600 * @param object $meta Current meta object.
602 if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
607 <wp:meta_key><?php echo wxr_cdata( $meta->meta_key ); ?></wp:meta_key>
608 <wp:meta_value><?php echo wxr_cdata( $meta->meta_value ); ?></wp:meta_value>