Export Form

Last Updated on July 3, 2019 by Neil Murray

WordPress has a built-in export feature which creates an export file in WordPress eXtended RSS or WXR XML format. It can contains posts, pages, custom post types, comments, custom fields, categories, tags, custom taxonomies, and users. There is also an import feature to upload the file.

Contact Form 7 provides a feature to export all forms. This is really helpful if we want to copy many CF7 forms to another site. But it’s not suitable for exporting a single CF7 form from a clients site.

WordPress export of Contact Form 7 forms

Background

When doing customer support, we’ve often had difficulties re-creating a client form for doing testing in our local WordPress installation. Many clients forms are large & include Multi pages with complex Logic; they are hard to follow & difficult & time consuming to re-create.

We’ve decided to add an optional feature to CF7 Skins to allow us export a single CF7 form directly from the CF7 editing screen.

WordPress Export

WordPress has the wp_export() function to generate content into XML format. Basically the function processes an array of post_ids and grabs all meta data related to each post id. Unfortunately here is no filter available currently to modify it, but we can modify the query before retrieving the post ids.

Method

  • Create the button.
  • Setup export_wp() parameters.
  • Modify SQL query.
  • Setup export file name.

1. Create the button

The easiest way to process our export function is to create an input that contains our specific data. In this case we set the input as a button, which will be submitted during CF7 save.

The input button can be placed anywhere within the CF7 Admin <form />. In this case we add the input button in the CF7 publishing section.

<input type="submit" name="cf7skins-export" class="button" value="Export Form" 
onclick="this.form.action.value = 'export'; return true;>

It uses cf7skins-export as the input name & export as the onclick value (to avoid the spinner being shown).

Avoid the spinner being shown

During CF7 save, the spinner button is always showing. It is based on the value of this.form.action.value if set to save. CF7 sets the value in Save button. On our case the click event is set to something else (i.e. export) to avoid the spinner showing.

2. Setup export parameters

As mentioned above, WordPress Export works based on post_ids array. To distinguish our export query, we need to pass our custom export parameter by adding post status cf7skins_export.

$args['content'] = 'page';
$args['status'] = 'cf7skins_export';
export_wp( $args ); // do the export process

Note: The post status accepts any status, even if it is not registered, but we need to use ‘post’ or ‘page’ post type.

3. Modify SQL query

Before retrieving the post ids array, we need to modify the default WordPress export query.

To check if the query is our export query, we have added a custom post status cf7skins_export before. If it is our query, we change ‘post status’ to the current form/post ID, and ‘page’ post type to wpcf7_contact_form CF7 post type.

// Check if this query is our export query.
// Query contains 'SELECT ID FROM' and 'cf7skins_export' string in it.
if ( strpos( $query, 'SELECT ID FROM' ) !== false && strpos( $query, 'cf7skins_export' ) ) {

	// Replace post type from page to wpcf7_contact_form
	$query = str_replace( ".post_type = 'page'", ".post_type = 'wpcf7_contact_form'", $query );

	// Replace post status to ID and set the value with current form ID
	$query = str_replace( ".post_status = 'cf7skins_export'", ".ID = '{$form_id}'", $query );						
}

Here is the default query generated by WP export with our custom arguments:

SELECT ID FROM wp_posts WHERE wp_posts.post_type = 'page' 
AND wp_posts.post_status = 'cf7skins_export'

It needs to be modified with CF7 post type and current form id as follows:

... wp_posts.post_type = 'wpcf7_contact_form' AND 
    wp_posts.ID = '1821'

4. Setup export file name

By default WordPress sets the exported filename as mysite.wordpress.2019-06-26.xml. We use a filter to modify the filename to cf7skins.mysite.2019-06-26.xml.