4

I've been seeing a feature of one click demo content installation option on some powerful WordPress themes now a days. Mostly on Themeforest themes.

I'd like to know if anyone have any idea about the best way to implement this. Or any other resources to do this. Some hints, idea or keyword would be suffice. I'm not asking you to write me the codes or something.

I'm wondering if you know the right way to do this.

Thanks in advance.

  • Create a container that holds demo contents (text file, json file, xml file, php array) with demo contents then use wp_insert_post() function with php loop to insert them. – Sisir Aug 19 '14 at 17:01
10

if you check the Wordpress importer plugin it is easy to implement this feature. But if you want the direct answer , here it is

First of all, we need to copy the Wordpress importer plugin files to our theme directory. like this

    1. themes/bootstrapguru_theme/inc/wordpress-importer.php
    2. themes/bootstrapguru_theme/inc/parser.php

you can find this plugin here wordpress importer

after this step , we are going to create 3 new files

File 1 : bootstrapguru-import.php paste the below code into that file

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 paste the below code into that file

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 paste the below code into that file

(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);

in the above file, you might see that we click on a button with .bootstrapguru_import class will fire a call back function in wordpress. which is created in file 2 and also on success we are appending the message to .import_message div

we are close to finish our exercise ;) lets tell the wordpress that we have new files by including single line in functions.php

include_once( 'inc/bootstrapguru-importer.php' );

so everything is set almost. Now create a button and div in your theme options panel in which button is to fire the import function and div is to post your successful import.

Let me know if you find any issues with my post, I might miss something. This worked for me after working on it for 6 hours

  • Wow. Thanks a lot. I was on the edge. You put something very helpful there. I'll let you know if it's done. Thanks again. – Muntasir Mahmud Aumio Aug 21 '14 at 20:14
  • I'm still having some problem with the js file. The post method of jquery ajax isn't working. It's not triggering the function in the backend. Do you have any idea why this's happening? – Muntasir Mahmud Aumio Aug 25 '14 at 12:42
  • It should work fine unless you have a javascript error. Check out this link for more information how to use ajax request on wordpress codex.wordpress.org/Plugin_API/Action_Reference/… – bootstrapguru Aug 26 '14 at 11:23
  • I was looking for a way to do that too, so I just want to thank BOOTSTRAPGURU for his helpful answer, and I'd like to share with you this repository on GitHub about exactly what you are looking for: One Click Demo Install for Wordpress Developers – Abdelhadi Jan 25 '15 at 9:24
0

Tried to comment, but my reputation isn't enough. AJAX part doesn't work because of not enqueuing .js file. Add the code below and it works perfectly.

function admin_import_scripts() {

    wp_register_script( 'bootstrapguru-import', get_template_directory_uri() . '/demo-importer/bootstrapguru-import.js', false, '1.0.0' );
    wp_enqueue_script( 'bootstrapguru-import' );
}
add_action( 'admin_enqueue_scripts', 'admin_import_scripts' );

protected by Pieter Goosen Feb 5 '16 at 8:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.