Combine and Process Multiple Data Elements into a Custom Export Field

Combine and Process Multiple Data Elements into a Custom Export Field

Custom export fields can be used to combine any number of data elements, function calls, and static text into the same export field.

Create a Custom Export Field

To create a custom export field, click Add Field and select Custom export field.

After selecting Custom export field, drag elements from the Available Data section on the right into the export field editor on the left. Unlike normal export fields, you can combine multiple fields together. For example, you can drag the First Name and Last Name elements to create a Full Name in a Users export.

Here's the result:

Using Static Data

You can include any type of static text or data required in your export. For example, you can include the currency symbol next to your prices when exporting products.

Here's the result:

Using PHP functions

You can use functions directly inside the Custom export field. Moreover, you can call functions with as many arguments as required.

Following the previous example, you can use a function to apply a 20% discount if the product price is above 100 USD and the product category is clothing. This function would take in two parameters: a price, and a category.

Here's the result:

Filtering with XPath

Filtering with XPath

In Step 2 of WP All Import, you can specify filtering options to only import records in your file that match your criteria.

Filtering Options

WP All Import filters your file using XPath. The visual filtering options designer, shown above, lets you visually construct an XPath expression.

First, add your filtering rules by specifying the rule criteria and clicking the Add Rule button.

Then click Apply Filters to XPath to generate an XPath expression.

You can also manually write an XPath expression to filter your file. Learn more at https://www.w3schools.com/xml/xpath_syntax.asp.

Radio Fields

Radio Fields

Radio fields allow the user to specify one of multiple, predefined values. These fields should all be defined in your add-on's constructor.

$this->add_on->add_field(
'property_type',
'Property Type',
'radio',
array(
'rent' => 'For Rent/Lease',
'buy' => 'For Sale'
)
);

The keys in the options array are the actual values that will be passed to the import function. The values are the labels that will be shown to the user in the interface:

Radio Field

If the values in the user』s XML/CSV file are different than the values required by your radio field, the user can use the built-in mapping interface to translate them:

Radio Field Mapping

Related

Add-On Structure

Text Fields

Image Fields

Nested Fields

Add Exported WordPress Data to Google Sheets using Zapier

Add Exported WordPress Data to Google Sheets using Zapier

Zapier can be used to send details of the export data towards Google Sheets. Connect Zapier to WP All Export and create the required trigger and action. The trigger would execute whenever a new export is found. The action would be sending the export information to Google Sheets.

Trigger: New Export File in WP All Export Pro

First, connect to Zapier and set up the required trigger: https://www.wpallimport.com/documentation/external-app-integration/.

Action: Create Spreadsheet Row in Google Sheets

Choose App & Event

Zapier will prompt to create an action once the trigger is done.

Select Google Sheets as the app and Create Spreadsheet Row as the event, then click Continue.

Choose Account

Select the Google Sheets account to use. If there's no account added, select + Connect a new account from the dropdown list and follow the steps.

Once an account has been selected, click Continue.

Set up action

This section allows configuring in which spreadsheet and worksheet the information will be added.

The following can be configured:

Drive: The Google Drive where the spreadsheet resides. The personal Google Drive is used if nothing is selected. Spreadsheet: Select which spreadsheet should be used by Zapier.Worksheet: Select which worksheet within the spreadsheet should be the data added to.

Once those fields have been selected, Zapier will load headers from the worksheet and allow to select which data is added into those columns. If there are no headers in the worksheet, Zapier will ask to set up the spreadsheet.

The following data can be added to the spreadsheet:

Export NameExport File URLExport Last Run DateRecords ExportedFile NameExport Created DateStatus CodeWebsite URLFile TypePost Types ExportedStatus MessageExport IDExport Trigger Type

Only details from the export can be added to the spreadsheet. Filling the Google Sheet rows with the data from the exported file is not possible with this integration. If that's the desired result, the Google Drive integration should be used instead.

Test action

Once the action has been configured, Zapier will attempt to test it. Select Skip Test to avoid this step.

Otherwise, click Test & Continue to check everything is working.

Once the test is successful, the zap can be enabled by clicking on Turn on Zap.

Import Processing

Import Processing

WP All Import splits your import into small, manageable chunks, and then imports each chunk individually.

This is to make it more likely that WP All Import will be able to get around script execution time limits and work on old, slow, or overcrowded servers, and to get around script.

On the Manage Imports -> Import Settings page, you can configure the way in which WP All Import splits your file into chunks and processes your import.

Configure Options

Iterative, Piece-By-Piece Processing

This option enables WP All Import to process the import in smaller pieces – known as 「iterations」. Each iteration must be able to process faster than any script execution time limits on your server.

The default 「In each iteration, process __ records」 setting should be lowered if you see an 「Import XML – Error」 midway through your import.

If WP All Import has been configured to download images, keep in mind that the amount of time for the images to download is counted towards the script execution time.

If you are processing 20 records per iteration, and each record has 5 associated images, that means your server must be able to download 20 * 5 = 100 images faster than any script execution time limits your server might have.

Import Speed

Think your import is running slower than it should be? Check out our guide on import speed.

The Structure of an Import Add-On

The Structure of an Import Add-On

WP All Import Add-Ons are WordPress plugins that make use of WP All Import's Rapid Add-On API. A few simple lines of code is all it takes to add UI elements to WP All Import and then determine how that data gets imported.

There are four main parts to an add-on. First, you need to register your code as a plugin to WordPress and then as an add-on to WP All Import. Next, you add UI elements for your user to drag and drop data into. Then you tell WP All Import how to import this new data. Finally, you under which circumstances the add-on should run.

Now we'll go through each part of a simple add-on for WP All Import.

1. Create a new WordPress plugin and include the rapid-addon.php file.

To get started, you need to tell WordPress that this is a plugin and then set the groundwork for our import add-on. Start with the WordPress plugin header and then instantiate a class for your add-on.

/*
Plugin Name: My First Add-On
Description: A super awesome add-on for WP All Import!
Version: 1.0
Author: WP All Import
*/
include "rapid-addon.php";

final class my_first_add_on {

protected static $instance;

protected $add_on;

static public function get_instance() {
if ( self::$instance == NULL ) {
self::$instance = new self();
}
return self::$instance;
}

2. Initialize the add-on.

Next, create a new instance of the RapidAddon class in the add-on's constructor. Pass the add-on』s name and slug to the RapidAddon's class constructor. The slug should be unique to the add-on and different than the add-on's class name.

protected function __construct() {

// Define the add-on
$this->add_on = new RapidAddon( 'My First Add-On', 'my_first_addon' );

3. Add UI fields to the import template.

When setting up an import the user will drag and drop their data into WP All Import. The code below will add two text fields to the add-on – one for Property Location, and one for Property Price.

$this->add_on->add_field('property_location', 'Property Location', 'text');
$this->add_on->add_field('property_address', 'Property Address', 'text');

After all of the UI fields are set up, define the import method for your add-on with the snippet below. Each time a post is imported or updated, WP All Import will call this method and pass the post ID and the imported data. You'll also need to initialize the add-on with init.

$this->add_on->set_import_function([ $this, 'import' ]);
add_action( 'init', [ $this, 'init' ] );

4. Tell WP All Import how to import data to your theme or plugin.

Here we will get the values of the two fields we created earlier and import them as custom fields.

public function import( $post_id, $data, $import_options, $article ) {

$fields = array(
'property_location',
'property_address'
);

foreach ( $fields as $field ) {
// Make sure the user has allowed this field to be updated.
if ( empty( $article['ID'] ) || $this->add_on->can_update_meta( $field, $import_options ) ) {

// Update the custom field with the imported data.
update_post_meta( $post_id, $field, $data[ $field ] );
}
}
}

5. Specify when your add-on runs.

Earlier we initialized our add-on with init. By default, add-ons will always run. You probably want to check to make sure a theme or plugin is active, or limit your add-on to specific custom post types.

public function init() {

$this->add_on->run( array( "themes" => array("Twenty Fourteen", "Twenty Fifteen") ) );

}

The above code will only allow the add-on to run if the Twenty Fourteen or Twenty Fifteen theme is active.

6. Close out the add-on.

Finally, we need to call the get_instance() method at the end of the file, outside of the class definition:

my_first_add_on::get_instance();

7. That』s all, you are done coding!

To see the add-on in action, install and activate this plugin, and do an import with WP All Import.

The add-on will appear in Step 3 of WP All Import as a box titled My First Add-On:

My First Add-On

Related

Text Fields

Radio Fields

Image Fields

Nested Fields

Best Practices

Best Practices

1. Follow an Object-Oriented design pattern.

You should use a singleton to ensure that your add-on works properly with WP All Import's WP-CLI integration. A singleton is a class that is limited to a single instance.

Example - ensuring a class has only a single instance

static public function get_instance() {
if ( self::$instance == NULL ) {
self::$instance = new self();
}
return self::$instance;
}

Be sure to instantiate the Rapid Add-On when the class is constructed:

$this->add_on = new RapidAddon( 'Yoast SEO Add-On', 'yoast_seo_add_on' );

Here's an overview showing how it all fits together:

/*
Plugin Name: WP All Import Yoast SEO Add-On
Description: A complete example add-on for importing data to certain Yoast SEO fields.
Version: 1.0
Author: WP All Import
*/

include "rapid-addon.php";

final class Yoast_SEO_Add_On {

protected static $instance;

protected $add_on;

static public function get_instance() {
if ( self::$instance == NULL ) {
self::$instance = new self();
}
return self::$instance;
}

protected function __construct() {

// Define the add-on
$this->add_on = new RapidAddon( 'Yoast SEO Add-On', 'yoast_seo_add_on' );

// Add UI elements to the import template
$this->add_on->add_field( 'yoast_wpseo_title', 'SEO Title', 'text' );

// This tells the add-on API which method to call
// for processing imported data.
$this->add_on->set_import_function( [ $this, 'import' ] );

// This registers the method that will be called
// to run the add-on.
add_action( 'init', [ $this, 'init' ] );
}

// Tell the add-on to run, add conditional statements as needed.
public function init() {

$this->add_on->run();

}

// Add the code that will actually save the imported data here.
public function import( $post_id, $data, $import_options ) {

}
}

Yoast_SEO_Add_On::get_instance();

2. Make the add-on run only when necessary.

Your add-on should only run when the user is importing to the theme or post types supported by your add-on.

Otherwise, your add-on』s import function would save data in the database when the user is doing an import that isn』t making use of your add-on.

Specify when your add-on runs using the run function.

This code is placed in the 'init' method as shown in section 1 above.

Example A – only when certain plugins are active

// This approach is needed when you need one OR another plugin active.
if ( function_exists('is_plugin_active') ) {
// Only run this add-on if the free or pro version of the Yoast plugin is active.
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) || is_plugin_active( 'wordpress-seo-premium/wp-seo-premium.php' ) ) {
$this->add_on->run();
}
}

// This approach works when you need one or more plugins active at the same time.
// Only run this add-on if BOTH the premium version of the Yoast plugin and WP All Export are active.
$this->add_on->run(array(
'plugins' => array(
'wordpress-seo-premium/wp-seo-premium.php',
'wp-all-export/wp-all-export.php'
)
));

}

Example B – only when importing to 「listing」 Custom Post Type

$this->add_on->run(
array(
"post_types" => array( "listing" ),
)
);

Example C – only when 「Twenty Fourteen」 OR 「Twenty Fifteen」 themes are activated

$this->add_on->run(
array(
"themes" => array( "Twenty Fourteen", "Twenty Fifteen" )
)
);

Example D – only when importing to 「post」 OR 「page」 Custom Post Type AND when 「Max Estate」 theme is activated AND when the "Max Estate Helper" plugin is activated

$this->add_on->run(
array(
"themes" => array( "Max Estate" ),
"post_types" => array( "post", "page" ),
"plugins" => array( "Max Estate Helper" )
)
);

Example E – run the add-on always

$this->add_on->run();

3. Make the add-on respect the user』s 「Choose which data to update…」 Import Settings.

On future runs of the same import, users can choose to not update Custom Fields, Images, and more.

Choose which data to update Add-On

The rapid add-on API supports checking if Custom Fields or images should be updated.

To do that, use the following methods. They should be placed in the 'import' method as shown in section 1 above.

Use can_update_meta to see if a Custom Field should be updated:

if ( $this->add_on->can_update_meta( '_yoast_wpseo_title', $import_options ) ) {
// Update the field if the user has allowed it in the import settings.
update_post_meta( $post_id, '_yoast_wpseo_title', $data['yoast_wpseo_title'] );
}

Use can_update_image to check of the 「Images」 box is checked:

if ( $this->add_on->can_update_image( $import_options ) ) {
// Retrieve the imported image's URL if images are set to be updated.
$image_url = wp_get_attachment_url( $data['yoast_wpseo_opengraph-image']['attachment_id'] );

// Save that image URL to our custom field.
update_post_meta( $post_id, '_yoast_wpseo_opengraph-image', $image_url );
}

There are more options in the Choose which data to update section, but they are rarely used by add-ons. These options are stored in the $import_options variable, which is passed to the registered import function. If your add-on does advanced things like changing parent posts, menu orders, etc. and you want to support these options, the settings you need to check are stored inside $import_options.

Please note can_update_image only tells you if the Images box is checked, it doesn』t tell you the settings. If you care, read it from $import_options.

4. Warn the user to install WP All Import.

You can display a dismissible notice warning the user to install WP All Import to use your add-on.

$this->add_on->admin_notice();

Customize the notice text by passing a string to admin_notice(), i.e.

$this->add_on->admin_notice(
'The Yoast WordPress SEO Add-On requires WP All Import Free and the Yoast WordPress SEO plugin.'
);

Add conditions for displaying the admin notice:

$this->add_on->admin_notice(
// Provide the notice text here.
"This Add-On requires WP All Import, the WP All Export Plugin and Twenty Fifteen theme.",
// This array should contain all of your conditions.
array(
// Multiple themes and plugins can be required.
"themes" => array( "Twenty Fifteen" ),
"plugins" => array( "wp-all-export/wp-all-export.php" )
)
);

5. Use tooltips when necessary to explain fields to users.

The fifth parameter of the add_field() function can take a string that will be used as a tooltip for the field. This will work for any of the field types.

$this->add_on->add_field(
// Field slug.
'property_price',
// Field display name.
'Price',
// Field type.
'text',
// Used for nested fields.
null,
// Set your tooltip text here.
'Numbers only, no symbols'
);

6. Add entries to the import log.

The log() function must be placed within the import function.

// Ensure it's either a new record or the settings allow updating images.
if ( empty( $article['ID'] ) || $this->add_on->can_update_image( $import_options ) ) {

// Retrieve the URL for our image.
$image_url = wp_get_attachment_url( $data['yoast_wpseo_opengraph-image']['attachment_id'] );

// Save our image URL as required by Yoast.
update_post_meta( $post_id, '_yoast_wpseo_opengraph-image', $image_url );

}

7. Disable the default images section if it』s not being used.

This should be placed within the add-on's constructor.

$this->add_on->disable_default_images();

Related

Overview

A Complete Add-On

Password Protected Files

Password Protected Files

WP All Import can import XML & CSV files that are protected by HTTP Authentication.

In Step 1, choose the Download  From URL option.

Then enter the URL to your file like so:

http://username:password@link-to-file

In the example below, you』ll find 3 parts in the link format.

http://louis:[email protected]/soflyy/feed.xml

Username: louisPassword: znxah&jfja_Link To File: some-site.com/soflyy/feed.xml

To find out if your password-protected file is using HTTP authentication, just enter the URL to it with the username and password specified in the above format into your browser.

If the file begins to show or download, it is using HTTP Authentication.

Text Fields

Text Fields

If you need users to be able to input text so they can import a string, use a text input field. This is useful when the possible values for the field aren』t constrained to a predefined list. These fields should all be defined in your add-on's constructor.

$this->add_on->add_field( 'property_address', 'Property Address', 'text', null, 'Tooltip', false, 'Default Text' );

Here』s what a text input field looks like:

Add-On Text Field

You can also add a textarea field:

$this->add_on->( 'field_name', 'Field Name', 'textarea', null, 'Tooltip', false, 'Default Text');

And, it will look like this:

Text Area Field

Display Text & Titles

Other times you might want to include title or text to explain things to users. You can use the following functions to add titles and text to the UI of your add-on.

$this->add_on->add_title( 'Title Text', 'Text that will appear as a tooltip next to the title.' );
$this->add_on->add_text( 'This is text that will appear as a normal paragraph.' );

Related

Add-On Structure

Radio Fields

Image Fields

Nested Fields

A Complete Add-On To Use As A Starting Point

A Complete Add-On To Use As A Starting Point

Use this add-on for importing to Yoast as a starting point for your own add-on. It provides a real-world example of how all of the add-on fields work together with WP All Import. No need to start from scratch, fork this example and modify it to your needs.

Here』s the complete add-on: https://github.com/soflyy/wp-all-import-example-addon

Here's the direct link to the .zip archive that you can install in WordPress: https://github.com/soflyy/wp-all-import-example-addon/archive/master.zip

Once you activate the add-on, you』ll be prompted to install WP All Import if it's not already installed and active on your site.

The add-on is for importing to various fields in Yoast WordPress SEO, so to see the result of what the add-on does you』ll need that plugin installed as well.

Do a new import with WP All Import Import (Download example-yoast-data.csv) – you』ll see the add-on show up in Step 3 of WP All Import Import.

Related

Overview

Best Practices