export_wp() – Generates the WXR export file for download.

You appear to be a bot. Output may be restricted

Description

Generates the WXR export file for download.

Default behavior is to export all content, however, note that post content will only be exported for post types with the can_export argument enabled. Any posts with the 'auto-draft' status will be skipped.

Usage

export_wp( $args );

Parameters

$args
( array ) optional – { Optional. Arguments for generating the WXR export file for download. Default empty array.
$content
( string ) optional – Type of content to export. If set, only the post content of this post type will be exported. Accepts 'all', 'post', 'page', 'attachment', or a defined custom post. If an invalid custom post type is supplied, every post type for which can_export is enabled will be exported instead. If a valid custom post type is supplied but can_export is disabled, then 'posts' will be exported instead. When 'all' is supplied, only post types with can_export enabled will be exported. Default 'all'.
$author
( string ) optional – Author to export content for. Only used when $content is 'post', 'page', or 'attachment'. Accepts false (all) or a specific author ID. Default false (all).
$category
( string ) optional – Category (slug) to export content for. Used only when $content is 'post'. If set, only post content assigned to $category will be exported. Accepts false or a specific category slug. Default is false (all categories).
$start_date
( string ) optional – Start date to export content from. Expected date format is 'Y-m-d'. Used only when $content is 'post', 'page' or 'attachment'. Default false (since the beginning of time).
$end_date
( string ) optional – End date to export content to. Expected date format is 'Y-m-d'. Used only when $content is 'post', 'page' or 'attachment'. Default false (latest publish date).
$status
( string ) optional – Post status to export posts for. Used only when $content is 'post' or 'page'. Accepts false (all statuses except 'auto-draft'), or a specific status, i.e. 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', or 'trash'. Default false (all statuses except 'auto-draft'). }

Returns

void

Source

File name: wordpress/wp-admin/includes/export.php


Lines:

1 to 100 of 648
function export_wp( $args = array() ) {
  global $wpdb, $post;

  $defaults = array(
    'content'    => 'all',
    'author'     => false,
    'category'   => false,
    'start_date' => false,
    'end_date'   => false,
    'status'     => false,
  );
  $args     = wp_parse_args( $args, $defaults );

  
/**
 * Fires at the beginning of an export, before any headers are sent.
 *
 * @since 2.3.0
 *
 * @param array $args An array of export arguments.
 */
  do_action( 'export_wp', $args );

  $sitename = sanitize_key( get_bloginfo( 'name' ) );
  if ( ! empty( $sitename ) ) {
    $sitename .= '.';
  }
  $date        = gmdate( 'Y-m-d' );
  $wp_filename = $sitename . 'WordPress.' . $date . '.xml';
  
/**
 * Filters the export filename.
 *
 * @since 4.4.0
 *
 * @param string $wp_filename The name of the file for download.
 * @param string $sitename    The site name.
 * @param string $date        Today's date, formatted.
 */
  $filename = apply_filters( 'export_wp_filename', $wp_filename, $sitename, $date );

  header( 'Content-Description: File Transfer' );
  header( 'Content-Disposition: attachment; filename=' . $filename );
  header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );

  if ( 'all' !== $args['content'] && post_type_exists( $args['content'] ) ) {
    $ptype = get_post_type_object( $args['content'] );
    if ( ! $ptype->can_export ) {
      $args['content'] = 'post';
    }

    $where = $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $args['content'] );
  } else {
    $post_types = get_post_types( array( 'can_export' => true ) );
    $esses      = array_fill( 0, count( $post_types ), '%s' );

    // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
    $where = $wpdb->prepare( "{$wpdb->posts}.post_type IN (" . implode( ',', $esses ) . ')', $post_types );
  }

  if ( $args['status'] && ( 'post' === $args['content'] || 'page' === $args['content'] ) ) {
    $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_status = %s", $args['status'] );
  } else {
    $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
  }

  $join = '';
  if ( $args['category'] && 'post' === $args['content'] ) {
    $term = term_exists( $args['category'], 'category' );
    if ( $term ) {
      $join   = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
      $where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d", $term['term_taxonomy_id'] );
    }
  }

  if ( in_array( $args['content'], array( 'post', 'page', 'attachment' ), true ) ) {
    if ( $args['author'] ) {
      $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_author = %d", $args['author'] );
    }

    if ( $args['start_date'] ) {
      $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date >= %s", gmdate( 'Y-m-d', strtotime( $args['start_date'] ) ) );
    }

    if ( $args['end_date'] ) {
      $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date < %s", gmdate( 'Y-m-d', strtotime( '+1 month', strtotime( $args['end_date'] ) ) ) );
    }
  }

  // Grab a snapshot of post IDs, just in case it changes during the export.
  $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} $join WHERE $where" );

  /*
	 * Get the requested terms ready, empty unless posts filtered by category
	 * or all content.
	 */
  $cats  = array();
  $tags  = array();
  $terms = array();
  if ( isset( $term ) && $term ) {

 View on GitHub View on Trac