The Duplicator Pro plugin lets developers access specific events during its execution cycles. Here’s a simple guide to the available hooks, which are supported in Duplicator Pro version 4.5.2 and above.
Hooks
Here are the hooks you can use with Duplicator Pro:
Hook | Description |
---|---|
duplicator_pro_build_before_start | Triggered right before the build process starts for both manual and scheduled builds. |
duplicator_pro_build_completed | Triggered at the very end of the build process for both manual and scheduled builds. |
duplicator_pro_build_fail | Triggered whenever a build fails for both manual and scheduled builds. |
duplicator_pro_first_login_after_install | Triggered after an installation has successfully run and the user accesses the WordPress admin for the first time. |
Setup
Hooks can be set up in different places. The quickest way to get started is by adding them to your theme’s functions.php file. You can also add hooks from custom or commercial plugins. Duplicator Pro hooks use the WordPress add_action call, so you can use them anywhere in the WordPress PHP stack.
Quick Start
To set up hooks and validate they are working with your WordPress site, follow these steps:
- Open your active theme’s functions.php file.
- Add the following code at the bottom of the file:
// Before build starts function callback
function __duplicator_pro_build_before_start_test_function() {
$var = "------------------------------------------------";
$var .= "Testing: duplicator_pro_build_before_start hook";
$var .= "------------------------------------------------";
error_log(print_r($var, true));
}
// Build completes function callback
function __duplicator_pro_build_completed_test_function() {
$var = "------------------------------------------------";
$var .= "Testing: duplicator_pro_build_before_start hook";
$var .= "------------------------------------------------";
error_log(print_r($var, true));
}
// Add Actions
add_action('duplicator_pro_build_before_start', '__duplicator_pro_build_before_start_test_function');
add_action('duplicator_pro_build_completed', '__duplicator_pro_build_completed_test_function');
3. Create a package to trigger the build hooks.
4. Open your PHP error or debug log file to see the results of the functions.
Samples
Below are some sample codes for various hooks. You can customize these examples to fit your needs.
Sample 1: Clean Up Transient Data Before Building a Package
This example shows how to clean up transient data before building a package.
function deleteAllTransientsOnPackageCreation() {
global $wpdb;
$sql = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_%"';
$wpdb->query($sql);
}
add_action('duplicator_pro_build_before_start', 'deleteAllTransientsOnPackageCreation');
Sample 2: Log Build Status to an External API (e.g., Slack)
This example shows how to log build status to an external API like Slack.
function sendSlackMessage($msg) {
// See Slack Webhook API (https://api.slack.com/messaging/webhooks)
$slackWebhook = 'https://hooks.slack.com/services/[XXX]/[YYY]/[ZZZ]';
$postfields = array('payload' => json_encode(array('text' => $msg)));
$ch = curl_init($slackWebhook);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_exec($ch);
curl_close($ch);
}
function sendSlackStartMessage(DUP_PRO_Package $package) {
sendSlackMessage("DUPLICATOR BUILD START [" . date("Y-m-d H:i:s") . "] Package name: " . $package->Name);
}
function sendSlackFailMessage(DUP_PRO_Package $package) {
sendSlackMessage("DUPLICATOR BUILD FAIL Package name: " . $package->Name);
}
function sendSlackCompleteMessage(DUP_PRO_Package $package) {
sendSlackMessage("DUPLICATOR BUILD COMPLETE [" . date("Y-m-d H:i:s") . "] Package name: " . $package->Name);
}
add_action('duplicator_pro_build_before_start', 'sendSlackStartMessage');
add_action('duplicator_pro_build_fail', 'sendSlackFailMessage');
add_action('duplicator_pro_build_completed', 'sendSlackCompleteMessage');
These samples demonstrate the flexibility and power of Duplicator Pro hooks. You can customize them to suit your specific needs and integrate them with other systems or services.