如果您检查Wordpress导入器插件,则很容易实现此功能。但是如果你想要直接的答案,就在这里
首先,我们需要将Wordpress导入器插件文件复制到主题目录。像这样
1. themes/bootstrapguru_theme/inc/wordpress-importer.php
2. themes/bootstrapguru_theme/inc/parser.php
您可以在此处找到此插件wordpress importer
完成此步骤后,我们将创建3个新文件
File 1 : bootstrapguru-import.php
将以下代码粘贴到该文件中
class bootstrapguru_import extends WP_Import
{
function check()
{
//you can add any extra custom functions after the importing of demo coment is done
}
}
File 2 : bootstrapguru-importer.php
将以下代码粘贴到该文件中
add_action( \'wp_ajax_my_action\', \'my_action_callback\' );
function my_action_callback()
{
global $wpdb;
if ( !defined(\'WP_LOAD_IMPORTERS\') ) define(\'WP_LOAD_IMPORTERS\', true);
// Load Importer API
require_once ABSPATH . \'wp-admin/includes/import.php\';
if ( ! class_exists( \'WP_Importer\' ) ) {
$class_wp_importer = ABSPATH . \'wp-admin/includes/class-wp-importer.php\';
if ( file_exists( $class_wp_importer ) )
{
require $class_wp_importer;
}
}
if ( ! class_exists( \'WP_Import\' ) ) {
$class_wp_importer = get_template_directory() ."/inc/wordpress-importer.php";
if ( file_exists( $class_wp_importer ) )
require $class_wp_importer;
}
if ( class_exists( \'WP_Import\' ) )
{
$import_filepath = get_template_directory() ."/tmp/demo.xml" ; // Get the xml file from directory
include_once(\'bootstrapguru-import.php\');
$wp_import = new bootstrapguru_import();
$wp_import->fetch_attachments = true;
$wp_import->import($import_filepath);
$wp_import->check();
}
die(); // this is required to return a proper result
}
File 3 : bootstrapguru-import.js
将以下代码粘贴到该文件中
(function($) {
"use strict";
$(\'.bootstrapguru_import\').click(function(){
$import_true = confirm(\'are you sure to import dummy content ? it will overwrite the existing data\');
if($import_true == false) return;
$(\'.import_message\').html(\' Data is being imported please be patient, while the awesomeness is being created :) \');
var data = {
\'action\': \'my_action\'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
$(\'.import_message\').html(\'<div class="import_message_success">\'+ response +\'</div>\');
//alert(\'Got this from the server: \' + response); <i class="fa fa-spinner fa-3x fa-spin"></i>
});
});
})(jQuery);
在上面的文件中,您可能会看到我们用
.bootstrapguru_import
类将在wordpress中启动回调函数。创建于
file 2 还有,关于成功,我们将附加信息
.import_message
部门
我们即将完成练习;)让我们告诉wordpress,通过在functions.php
include_once( \'inc/bootstrapguru-importer.php\' );
所以几乎一切都安排好了。现在在你的主题选项面板中创建一个按钮和div,按钮启动导入功能,div发布成功的导入。
如果你发现我的帖子有任何问题,请告诉我,我可能会错过一些东西。我花了6个小时才完成