Overview
The Duplicator Pro plugin allows for developers to hook into certain events that happen during its execution cycles. Below is an overview of the currentlysupported hooks. Hooks are officially supported in Duplicator Pro version 4.5.2+
Hooks
The following hooks are currently available for Duplicator Pro
Hook | Description |
---|---|
duplicator_pro_build_before_start | Called right before the build process starts for both manual builds and scheduled builds. |
duplicator_pro_build_completed | Called at the very end of the build process for both manual builds and scheduled builds. |
duplicator_pro_build_fail | This hook is called whenever a build fails for both manual builds and scheduled builds. |
duplicator_pro_first_login_after_install | Called after an install has successfully ran and user accesses WordPress admin for the first time. |
Setup
Hooks can be called from several different places. The quickest way to get started with hooks is via thethemes functions.php file found in your themes directory. Duplicator Pro hooks can also be calledfrom custom or commercial plugins. Duplicator Pro hooks make useof the WordPress API add_action call. So basically anywhere in the WordPress PHP stack thatmakes use of the add_action method users can call into the Duplicator Pro hooks shown above.
QUICK START
To get started with hooks and validate they are working properly with your WordPress site follow these quick setup steps.
2. Paste the following code at the bottom of the file
//Before build starts function callbackfunction __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 callbackfunction __duplicator_pro_build_completed_test_function() {$var = "------------------------------------------------";$var .= "Testing: duplicator_pro_build_before_start hook";$var .= "------------------------------------------------";error_log(print_r($var, true));}//Add Actionsadd_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 and the results of the functions should be shown.
SAMPLES
Below are some samples that can be used with the various hooks. Because the code is customizable the possibilities are endless.
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-2In this example you can log build statues to basically any external API you want. In this example we are posting to 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');