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

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注