How to Trigger Cloning from Custom Code or Another Plugin
Version 4.4.1 and above
Use the introduced function ns_cloner_perform_clone that is only available from version 4.4.1 and above. Parameters are passed to the function as an array. For example:
ns_cloner_perform_clone( array( 'source_id' => 1, 'target_name' => 'sample-target', 'target_title' => 'Sample Target Title', ) );
| Parameter | Type | Description |
|---|---|---|
| clone_mode | string | Required. The clone mode. Default 'core'. Accepts 'core', 'clone_over', 'search_replace', 'clone_teleport'. |
| source_id | integer | Required. The source site id. |
| user_id | integer | Optional. The user id to assign to the site. |
| target_name | string | Required. The new site subdomain or sub directory. |
| target_title | string | Required. The source site title. |
| tables_to_clone | array | Optional. The source site tables to clone. These should have the prefix. |
| do_copy_posts | integer | Optional. Copy posts. Default 1. Accepts 1, 0. Set to 1 to copy and 0 not to copy. |
| post_types_to_clone | array | Optional. Post types to clone. Defaults to none. Set as 'all' for all post types |
| debug | integer | Optional. Default 0. Accepts 1, 0. Set to 1 to debug and 0 not to debug. |
| do_copy_files | integer | Optional. Defaults to 0. Set to 1 to copy files from source site to target site |
The function returns a response of WP_Error or an array when successful with a message. Sample cloning:
function my_custom_plugin_maybe_clone( $source_id, $target_name, $target_title, $user_id ) {
if ( ! function_exists( 'ns_cloner_perform_clone' ) ) {
return [
'success' => false,
'message' => 'Cloner needs to be active';
];
}
$response = ns_cloner_perform_clone( array(
'source_id' => $source_id,
'target_name' => $target_name,
'target_title' => $target_title,
'user_id' => $user_id,
) );
if ( is_wp_error( $response ) ) {
return [
'success' => false,
'message' => $response->get_error_message();
];
}
return [
'success' => true,
'message' => $response['message']
];
}
Other cloning methods:
$array_of_post_types = array(
'attachment',
'page',
'post',
'product',
'revision',
'wp_global_styles',
);
$array_of_tables = array(
'wp_commentmeta',
'wp_comments',
'wp_links',
'wp_options',
'wp_postmeta',
'wp_posts',
'wp_term_relationships',
'wp_term_taxonomy',
'wp_termmeta',
'wp_terms',
);
/**
* Set up the cloning request.
* These are the same fields that get submitted via an AJAX request when cloning via the
* admin interface, so you can inspect that request to determine other ways to configure
* the parameters, particularly if you're using NS Cloner Pro and want to use another
* clone mode (like clone-over or teleport / remote cloning)
*/
$request = array(
'clone_mode' => 'core',
'source_id' => 1, // any blog/site id on network
'user_id' => 1, // Optional user id to assign to the site
'target_name' => 'subdomain-or-subdir',
'target_title' => 'New Site Title',
'tables_to_clone' => $array_of_tables,
'do_copy_posts' => 1,
'post_types_to_clone' => $array_of_post_types, // can customize post types . . .
'debug' => 1,
'clone_nonce' => wp_create_nonce( 'ns_cloner' ),
);
// This is required to bootstrap the required plugin classes.
ns_cloner()->init();
foreach ( $request as $key => $value ) {
ns_cloner_request()->set( $key, $value );
}
ns_cloner_request()->set_up_vars();
ns_cloner_request()->save();
// Run init to begin. This will run in the background
ns_cloner()->process_manager->init();
Trigger from plugins
Gravity Forms
add_action( 'gform_after_submission', 'create_custom_site_after_submission', 10, 2 );
function create_custom_site_after_submission( $entry, $form ) {
if ( ! function_exists( 'ns_cloner_perform_clone' ) ) {
return;
}
$target_name = rgar( $entry, '1' ); // Change with your id.
$target_title = rgar( $entry, '2' ); // Change with your id.
$response = ns_cloner_perform_clone( array(
'source_id' => 1,
'target_name' => $target_name,
'target_title' => $target_title,
) );
if ( ! is_wp_error( $response ) ) {
GFCommon::log_debug( 'cloner_site_error: result => ' . $response->get_error_message() );
}
GFCommon::log_debug( 'cloner_site_success: result => ' . print_r( $response, true ) );
}