From “aha” to “oh shit”, learn how to use WordPress like a Pro

Subscribe to get our latest content by email.

Powered by ConvertKit
Tutorials

Rara Theme One Click Demo Import Plugin for Developers

Rara One Click Demo Import plugin will help your theme users to easily install the demo settings of the respective theme.

You can predefine the import by including the demo files locally in the theme files or by predefining the demo import URL or by giving your users separate downloadable .xml, .wie, .json and .dat files.

How to create XML file of a demo

If your theme users ask for demo contents, you can give them XML files. You can create XML file using the following plugin:

https://wordpress.org/plugins/wordpress-importer/

How to create WIE or JSON file of a demo

You can also give the widgets settings of your demo to your users. You can export widgets settings using the following plugin:

https://wordpress.org/plugins/widget-importer-exporter/

How to create DAT file of a demo

You can also give the customizer settings of the theme’s demo. You can export customizer settings using the following plugin:

https://wordpress.org/plugins/customizer-export-import/

How to predefine demo imports using URL?

Add the following code structure, with your own values on your theme (using the rrdi/import_files filter):

function rrdi_import_files() {

return array(

array(

'import_file_name' => 'Demo Import 1',

'import_file_url' => 'http://www.your_domain.com/rrdi/demo-content.xml',

'import_widget_file_url' => 'http://www.your_domain.com/rrdi/widgets.json',

'import_customizer_file_url' => 'http://www.your_domain.com/rrdi/customizer.dat',

'import_preview_image_url' => 'http://www.your_domain.com/rrdi/preview_import_image1.jpg',

'import_notice' => __( 'After you import this demo, you will have to setup the slider separately.', 'your-textdomain' ),

),

array(

'import_file_name' => 'Demo Import 2',

'import_file_url' => 'http://www.your_domain.com/rrdi/demo-content2.xml',

'import_widget_file_url' => 'http://www.your_domain.com/rrdi/widgets2.json',

'import_customizer_file_url' => 'http://www.your_domain.com/rrdi/customizer2.dat',

'import_preview_image_url' => 'http://www.your_domain.com/rrdi/preview_import_image2.jpg',

'import_notice' => __( 'A special note for this import.', 'your-textdomain' ),

),

);

}

add_filter( 'rrdi/import_files', 'rrdi_import_files' );

You can set content, widgets, and customizer import files. You can also define a preview image, which will be used only when multiple demo imports are defined, so that the user will see the difference between the imports.

How to automatically assign “Front page”, “Posts page” and menu locations after the import is complete?

You can do that, with the rrdi/after_import action hook. The code would look something like this:

function rrdi_after_import_setup() {

// Assign menus to their locations.

$main_menu = get_term_by( 'name', 'Top Menu', 'nav_menu' );

set_theme_mod( 'nav_menu_locations', array(

'primary-menu' => $main_menu->term_id,

)

);

// Assign front page and posts page (blog page).

$front_page_id = get_page_by_title( 'Home' );

$blog_page_id = get_page_by_title( 'Blog' );

update_option( 'show_on_front', 'page' );

update_option( 'page_on_front', $front_page_id->ID );

update_option( 'page_for_posts', $blog_page_id->ID );

}

add_action( 'rrdi/after_import', 'rrdi_after_import_setup' );

How to predefine demo imports using local import files (from theme folder)?

You have to use the same filter as in above example, but with a slightly different array keys: local_*. The values have to be absolute paths (not URLs) of your import files. To use local import files that reside in your theme folder, use the below code.

Note: Make sure your import files are readable!

function rrdi_import_files() {

return array(

array(

'import_file_name' => 'Demo Import 1',

'local_import_file' => trailingslashit( get_template_directory() ) . 'rrdi/demo-content.xml',

'local_import_widget_file' => trailingslashit( get_template_directory() ) . 'rrdi/widgets.json',

'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'rrdi/customizer.dat',

'import_preview_image_url' => 'http://www.your_domain.com/rrdi/preview_import_image1.jpg',

'import_notice' => __( 'After you import this demo, you will have to setup the slider separately.', 'your-textdomain' ),

),

array(

'import_file_name' => 'Demo Import 2',

'local_import_file' => trailingslashit( get_template_directory() ) . 'rrdi/demo-content2.xml',

'local_import_widget_file' => trailingslashit( get_template_directory() ) . 'rrdi/widgets2.json',

'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'rrdi/customizer2.dat',

'import_preview_image_url' => 'http://www.your_domain.com/rrdi/preview_import_image2.jpg',

'import_notice' => __( 'A special note for this import.', 'your-textdomain' ),

),

);

}

add_filter( 'rrdi/import_files', 'rrdi_import_files' );

How do I handle different “after import setups” depending on which predefined import was selected?

You can implement different after import setups for multiple predefined demo imports. Let’s say, you have predefined two demo imports with the following names: ‘Demo Import 1’ and ‘Demo Import 2’, the code for after import setup would be (using the rrdi/after_import filter):

function rrdi_after_import( $selected_import ) {

echo "This will be displayed on all after imports!";

if ( 'Demo Import 1' === $selected_import['import_file_name'] ) {

echo "This will be displayed only on after import if user selects Demo Import 1";

// Set logo in customizer

set_theme_mod( 'logo_img', get_template_directory_uri() . '/assets/images/logo1.png' );

}

elseif ( 'Demo Import 2' === $selected_import['import_file_name'] ) {

echo "This will be displayed only on after import if user selects Demo Import 2";

// Set logo in customizer

set_theme_mod( 'logo_img', get_template_directory_uri() . '/assets/images/logo2.png' );

}

}

add_action( 'rrdi/after_import', 'rrdi_after_import' );

How to add some code before the widgets get imported?

You can use the rrdi/before_widgets_import action for this. You can also target different predefined demo imports like in the example above. Here is a simple example code of the rrdi/before_widgets_import action:

function rrdi_before_widgets_import( $selected_import ) {

echo "Add your code here that will be executed before the widgets get imported!";

}

add_action( 'rrdi/before_widgets_import', 'rrdi_before_widgets_import' );

How to change the plugin intro text?

You can change the plugin intro text by using the rrdi/plugin_intro_text filter:

function rrdi_plugin_intro_text( $default_text ) {

$default_text .= '<div class="RRDI__intro-text">This is a custom text added to this plugin intro text.</div>';

return $default_text;

}

add_filter( 'rrdi/plugin_intro_text', 'rrdi_plugin_intro_text' );

To add some text in a separate “box”, you should wrap your text in a div with a class of ‘RRDI__intro-text’, like in the code example above.

How to disable generation of smaller images (thumbnails) during the content import?

Smaller images will greatly improve the time needed to import the content (images), but only the original sized images will be imported. You can disable it with a filter, so just add this code to your theme function.php file:

add_filter( 'rrdi/regenerate_thumbnails_in_content_import', '__return_false' );

How to change the location, title and other parameters of the plugin page?

If you do not like the location of the “Rara Demo Import” plugin page in *Appearance -> Rara Demo Import*, you can change that with the filter below. Apart from the location, you can also change the title or the page/menu, and some other parameters as well.

function rrdi_plugin_page_setup( $default_settings ) {

$default_settings['parent_slug'] = 'themes.php';

$default_settings['page_title'] = esc_html__( 'Rara One Click Demo Import' , 'rara-demo-import' );

$default_settings['menu_title'] = esc_html__( 'Rara Demo Import' , 'rara-demo-import' );

$default_settings['capability'] = 'import';

$default_settings['menu_slug'] = 'rara-demo-import';

return $default_settings;

}

add_filter( 'rrdi/plugin_page_setup', 'rrdi_plugin_page_setup' );

How to customize the message that appears before importing the data?

Use the filter:

apply_filters( 'rrdi_before_import_msg', $msg);

From “aha” to “oh shit”, learn how to use WordPress like a Pro

Subscribe to get our latest content by email.

Powered by ConvertKit

Leave a Reply

Required fields are marked*