admin管理员组文章数量:1320882
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!
本文标签: wordpressUnable to import WXR via PHP on plugin activationStack Overflow
版权声明:本文标题:wordpress - Unable to import WXR via PHP on plugin activation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742087246a2420053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论