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 are all it takes to add UI elements to WP All Import and then determine how data gets imported.
There are five main parts to an add-on:
- Step 1: Register as a Plugin and Add-On
- Step 2: Check Required Classes and Dependencies
- Step 3: Initialize the Add-On
- Step 4: Add the Import Fields and UI Elements
- Step 5: Close Out The Add-On.
Now, we'll go through each of them and learn how to create a simple add-on for WP All Import.
Important Note –The this is the latest Rapid Add-On structure and it requires WP All Import (Free) v3.7.6 or higher, or WP All Import Pro v4.8.9-beta-1.0 or higher. If you're using an older version, please check Rapid Add-On Legacy Structure
Step 1: Register as a Plugin and Add-On
To get started, you need to tell WordPress that this is a plugin and then set the groundwork for our import add-on. Start by defining your plugin with metadata and setting up a class structure. This involves creating the plugin header and using a singleton pattern. We'll instantiate a class for our add-on and reference the Rapid Add-On API from WP All Import —this will make it work with both the Free and Pro versions of WP All Import.
/*
Plugin Name: My First Add-On
Description: A super awesome add-on for WP All Import!
Version: 1.0
Author: WP All Import
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
final class My_First_Add_On {
protected static $instance;
protected $add_on;
protected $addon_name = 'My First Add-On'; // Define the name of your add-on
protected $addon_slug = 'my_first_addon'; // Define a unique add-on slug
// Singleton pattern to ensure only one instance of the add-on is loaded
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
// Constructor to add the init hook
protected function __construct() {
add_action('init', array($this, 'init'));
}
}
Here, the plugin header gives WordPress the basic details about your plugin. The singleton class ensures that only one instance of the add-on exists at any time. We add an action to the init
method to prepare our add-on for initialization when WordPress is ready.
Step 2: Check Required Classes and Dependencies
Before moving forward, it's important to verify that WP All Import and its Rapid Add-On API are active. If these essential classes are missing, we won't be able to initiate our add-on.
public function init() {
// Check for required classes and display an admin notice if not found
if (!class_exists('PMXI_Plugin') || !class_exists('PMXI_RapidAddon')) {
add_action('admin_notices', function() {
echo 'The My First Add-On requires WP All Import Pro to be installed and active.
';
});
return;
}
}
This part of the code checks for the existence of the PMXI_Plugin
or PMXI_RapidAddon
required classes. If they're not found, it triggers an admin notice to inform the user that WP All Import is needed. This check ensures that everything is in place before proceeding.
Optionally, you can also limit your add-on to run only when specific plugins or themes are active by adding more logic to the init
method. Here is an example of running the add-on only when WP All Import is enabled and the Twenty Twenty-Four theme is active.
public function init() {
// Check for required classes and display an admin notice if not found
if (!class_exists('PMXI_Plugin') || !class_exists('PMXI_RapidAddon')) {
add_action('admin_notices', function() {
echo 'The My First Add-On requires WP All Import Pro to be installed and active.
';
});
return;
}
// Check if the Twenty Twenty-Four theme is active
$theme = wp_get_theme();
if (!in_array($theme->get('Name'), ['Twenty Twenty-Four'])) {
add_action('admin_notices', function() {
echo 'The My First Add-On requires the Twenty Twenty-Four theme to be active.
';
});
return;
}
Step 3: Initialize the Add-On
Now that we know the required classes are available, it's time to initialize the add-on with the unique name and slug we defined in Step 1.
// Initialize the add-on with its name and slug
$this->add_on = new PMXI_RapidAddon($this->addon_name, $this->addon_slug);
$this->wpai_setup_fields(); // Add fields to the add-on
$this->add_on->set_import_function([$this, 'import']); // Set the import function
$this->add_on->run(); // Run the add-on
Here is how our init
method should look like:
public function init() {
// Check for required classes and display an admin notice if not found
if (!class_exists('PMXI_Plugin') || !class_exists('PMXI_RapidAddon')) {
add_action('admin_notices', function() {
echo 'The My First Add-On requires WP All Import Pro to be installed and active.
';
});
return;
}
// Initialize the add-on with its name and slug
$this->add_on = new PMXI_RapidAddon($this->addon_name, $this->addon_slug);
$this->wpai_setup_fields(); // Add fields to the add-on
$this->add_on->set_import_function([$this, 'import']); // Set the import function
$this->add_on->run(); // Run the add-on
}
Step 4: Add the Import Fields and UI Elements
Next, we'll add custom fields to the import template. These fields let users map their data using WP All Import's drag and drop interface.
// Define the fields for the import template
public function wpai_setup_fields() {
$this->add_on->add_field('property_location', 'Property Location', 'text');
$this->add_on->add_field('property_address', 'Property Address', 'text');
}
In this example, we added the property_location
and property_address
custom fields to the import template. Now, these two are text fields, but you can also add radio fields, image fields, and complex nested fields to your add-on.
Next, we'll specify how the data should be imported into your theme or plugin. This function will handle the actual data import.
// Import function to handle the actual data import
public function import($post_id, $data, $import_options) {
$fields = ['property_location', 'property_address'];
foreach ($fields as $field) {
// Check if the field can be updated
if (empty($data['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]);
}
}
}
It will update the post meta with the imported values and ensure that each field can be updated before saving the data, making sure that only allowed fields are modified during the import process.
Import Any CSV, XML, or Excel to WordPress
- Any theme or plugin
- Images & galleries
- Custom fields
- Categories & tags
- Woo, ACF, Meta Box, JetEngine
Step 5: 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();
That’s All — You Are Done Coding!
By calling get_instance()
, we ensure the add-on is properly initialized and ready to run. This final step completes the setup, making your WP All Import add-on fully functional. Adjust field names and import logic as needed for your specific use case.
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:
Here is an example of a fully functional add-on for WP All Import if you want to see what it looks like.
Related Docs
Learn more about text fields for WP All Import Add-Ons.
Learn more about using radio fields for WP All Import Add-Ons.
Learn more about image fields in your WP All Import Add-On.
Learn about adding nested fields to your WP All Import Add-On.