Enhancements to the Import/Export feature in WordPress 5.7

In WordPress 5.7, two main changes affected the WordPress built-in import/export feature.

A new export specific filterFilter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. for post title

WP 5.7 introduces creates an export-specific filter for post titles.

Since WordPress 2.5 and 2.6, post_content and post_excerpt have both had export-specific filters: the_content_export, and the_excerpt_export, respectively. post_title, however, has used the_title_rss, which behaves differently in two important ways:

  • It strips HTMLHTML HyperText Markup Language. The semantic scripting language primarily used for outputting content in web browsers. tags from the string
  • It HTML-encodes the title string

These behaviors are not ideal for exports, since it changes the post title, resulting in data loss in export files, and incorrect post duplicate matching on import. This changes replaces the usage of the_title_rss with a new filter, the_title_export. The new filter is intended to be used in the same as the_content_export and the_excerpt_export.

Usage example: add a prefix to exported posts titles:

function wporg_edit_exported_title( $post_title ) {
	$post_title = sprintf(
		/* Translators: the post title. */
		__( '[IMPORTED] %s', 'text-domain' ),
		$post_title
	);
	return $post_title;
}
add_filter( 'the_title_export', 'wporg_edit_exported_title', 10 );

For reference, see ticketticket Created for both bug reports and feature development on the bug tracker. #52250.

Add the date each post was last modified in export files

WP 5.7 introduces post_modified and post_modified_gmt fields to the generated WXR export file.

This allows for more flexibility when determining which version of a post is the latest one, and makes it possible to implement import logic involving updating and adding revisionsRevisions The WordPress revisions system stores a record of each saved draft or published update. The revision system allows you to see what changes were made in each revision by dragging a slider (or using the Next/Previous buttons). The display indicates what has changed in each revision. to existing posts or pages.

Here is a sample of the new structure of RSS feeds:

<item>
	<title><?php echo $title; ?></title>
	<link><?php the_permalink_rss(); ?></link>
	<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
	<dc:creator><?php echo wxr_cdata( get_the_author_meta( 'login' ) ); ?></dc:creator>
	<guid isPermaLink="false"><?php the_guid(); ?></guid>
	<description></description>
	<content:encoded><?php echo $content; ?></content:encoded>
	<excerpt:encoded><?php echo $excerpt; ?></excerpt:encoded>
	<wp:post_id><?php echo (int) $post->ID; ?></wp:post_id>
	<wp:post_date><?php echo wxr_cdata( $post->post_date ); ?></wp:post_date>
	<wp:post_date_gmt><?php echo wxr_cdata( $post->post_date_gmt ); ?></wp:post_date_gmt>
	<wp:post_modified><?php echo wxr_cdata( $post->post_modified ); ?></wp:post_modified>
	<wp:post_modified_gmt><?php echo wxr_cdata( $post->post_modified_gmt ); ?></wp:post_modified_gmt>
	<wp:comment_status><?php echo wxr_cdata( $post->comment_status ); ?></wp:comment_status>
	<wp:ping_status><?php echo wxr_cdata( $post->ping_status ); ?></wp:ping_status>
	<wp:post_name><?php echo wxr_cdata( $post->post_name ); ?></wp:post_name>
	<wp:status><?php echo wxr_cdata( $post->post_status ); ?></wp:status>
	<wp:post_parent><?php echo (int) $post->post_parent; ?></wp:post_parent>
	<wp:menu_order><?php echo (int) $post->menu_order; ?></wp:menu_order>
	<wp:post_type><?php echo wxr_cdata( $post->post_type ); ?></wp:post_type>
	<wp:post_password><?php echo wxr_cdata( $post->post_password ); ?></wp:post_password>
	<wp:is_sticky><?php echo (int) $is_sticky; ?></wp:is_sticky>
</item>

For reference, see ticket #52180.

Better performance for menu items imports

The wp_update_nav_menu_item() function now makes use of wp_resolve_post_date() when updating menu items. This allows a menu item’s post_date to be set to a specific value instead of just “now”. This allows the WordPress Importer to perform faster, more accurate duplicate checks.

For reference, see #52189.


Props @desrosj and @chaton666 for proofreading.

#5-7, #dev-notes