0

I'm setting up a large collection of sites for my non-profits chapters. They all need to start with a bit of boilerplate content and I'm hoping to automate the import process. However, I can't for the life of me get the importer to work programmatically. It seems I can't get WP_Import to load.

To simplify debugging, I moved just the import portion into its own plugin. It fails at line 42 (Checking for WP_Import and dying if it doesn't exist), which is consistent with my difficult getting ahold of WP_IMPORT.

<?php
/*
Plugin Name: WXR Import on Activation
Description: Automatically imports a WXR file into WordPress when the plugin is activated.
Version: 1.9
Author: Troutoil
*/

// Hook to run on plugin activation
register_activation_hook(__FILE__, 'import_wxr_on_activation');

function import_wxr_on_activation() {
    // Path to the WXR file
    $wxr_file_path = plugin_dir_path(__FILE__) . 'boilerplate.xml';

    // Check if the WXR file exists
    if (!file_exists($wxr_file_path)) {
        error_log('WXR file not found at: ' . $wxr_file_path);
        return;
    }

    // Ensure the WordPress Importer plugin is installed and activated
    if (!function_exists('is_plugin_active')) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    $importer_plugin_slug = 'wordpress-importer/wordpress-importer.php';

    if (!is_plugin_active($importer_plugin_slug)) {
        wp_die('The WordPress Importer plugin must be installed and activated to run this script.');
    }

    // Load the WordPress Importer plugin directly
    $importer_path = WP_PLUGIN_DIR . '/wordpress-importer/wordpress-importer.php';
    if (!file_exists($importer_path)) {
        wp_die('The WordPress Importer plugin files are missing.');
    }
    require_once $importer_path;

    // Ensure the necessary class exists
    if (!class_exists('WP_Import')) {
        wp_die('Failed to load the WordPress Importer classes.');
    }

    // Run the importer directly
    $importer = new WP_Import();
    $importer->fetch_attachments = true; // Enable attachment import

    try {
        ob_start();
        $importer->import($wxr_file_path);
        $output = ob_get_clean();

        // Log the output for debugging
        error_log('WXR Import Output: ' . $output);

        // Set a success flag for admin notice
        update_option('wxr_import_success', true);
    } catch (Exception $e) {
        error_log('WXR Import Exception: ' . $e->getMessage());
    }
}

// Admin notice to inform if the import was successful
add_action('admin_notices', function() {
    if (get_option('wxr_import_success', false)) {
        echo '<div class="notice notice-success is-dismissible">
                <p>WXR file has been successfully imported.</p>
              </div>';
        delete_option('wxr_import_success');
    }
});

Any ideas would be greatly appreciated. I've been pulling my hair out for days!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.