Skip to content

Instantly share code, notes, and snippets.

@juanlistab
Last active August 23, 2024 17:38
Show Gist options
  • Save juanlistab/95d0056b777dff54767b5f684b5408e0 to your computer and use it in GitHub Desktop.
Save juanlistab/95d0056b777dff54767b5f684b5408e0 to your computer and use it in GitHub Desktop.
Yoast Example Add-On

Creating a WP All Import Add-On

Introduction

Creating a WP All Import add-on is a simple and rewarding process. By following this guide, you'll learn how to integrate custom fields and define import logic, ensuring everything works seamlessly with WP All Import.

Step 1: Register as a Plugin and Add-On

Let's start by defining your plugin with some essential metadata and setting up a class structure. This step involves creating the plugin header and using a singleton pattern to make sure only one instance of the add-on is loaded.

/*
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';
    protected $addon_slug = 'my_first_addon';

    // 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. By adding an action to init, we prepare to initialize our add-on when WordPress is ready.

Step 2: Check Required Classes

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 want to let the user know.

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 '<div class="error notice"><p>The My First Add-On requires WP All Import Pro to be installed and active.</p></div>';
        });
        return;
    }
}

This part of the code checks for the existence of required classes. If they're not found, it triggers an admin notice to inform the user that WP All Import Pro is needed. This check ensures that everything is in place before proceeding.

Step 3: Initialize the Add-On

Now that we know the required classes are available, it's time to initialize the add-on with a unique name and slug.

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 '<div class="error notice"><p>The My First Add-On requires WP All Import Pro to be installed and active.</p></div>';
        });
        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
}

Here, we initialize the add-on using the PMXI_RapidAddon class, providing it with a unique name and slug. This step is crucial for setting up the add-on correctly.

Step 4: Add UI Elements

Next, we add custom fields to the import template. These fields allow users to map their data, making the import process more flexible.

// 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 step, we add custom fields like property_location and property_address to the import template. These fields will be available for users to map their data during the import process, making the add-on more functional.

Step 5: Define Import Logic

Now, 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]);
        }
    }
}

This function updates the post meta with the imported values. It ensures each field can be updated before saving the data, making sure that only allowed fields are modified during the import process.

Step 6: Initialize the Add-On

Finally, we need to ensure the add-on runs under the right circumstances. We'll call the get_instance() method to initialize it.

// Get the instance of the add-on to initialize it
My_First_Add_On::get_instance();

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.

By following these steps, you can create a WP All Import add-on that integrates seamlessly with your WordPress site. Enjoy the flexibility and power of custom imports tailored to your needs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment