uo_quiz_certificate
Fires after a quiz certificate has been generated, providing access to all generated data.
add_action( 'uo_quiz_certificate', $callback, 10, 6 );
Description
Fires after a user's quiz certificate PDF has been generated and saved. Developers can use this hook to perform actions like sending an email notification with the certificate, logging the certificate generation, or modifying the certificate file before it's finalized. The hook provides access to the file path, quiz details, results, certificate ID, current user, and generation timestamp.
Usage
add_action( 'uo_quiz_certificate', 'your_function_name', 10, 6 );
Parameters
-
$path_on_server(mixed) - The server path to the generated certificate file.
-
$quiz_post(mixed) - The `$path_on_server` parameter contains the file path on the server where the generated certificate is saved.
-
$quiz_results(mixed) - This parameter represents the WordPress post object for the quiz that was completed.
-
$certificate_id(mixed) - This parameter contains the completion percentage or score of the quiz results for the current user.
-
$current_user(mixed) - The `$certificate_id` parameter holds the ID of the certificate that has been generated or is about to be generated for the quiz.
-
$current_time(mixed) - This parameter contains the WordPress `WP_User` object for the currently logged-in user who completed the quiz.
Examples
<?php
/**
* Example function to hook into the 'uo_quiz_certificate' action.
* This function could, for instance, send an email notification
* with the generated certificate attached.
*
* @param string $path_on_server The absolute server path to the generated certificate PDF.
* @param WP_Post $quiz_post The WordPress post object for the quiz.
* @param array $quiz_results The results of the user's quiz attempt.
* @param int $certificate_id The ID of the certificate post.
* @param WP_User $current_user The WordPress user object for the current logged-in user.
* @param string $current_time The formatted current time.
*/
function my_custom_quiz_certificate_handler( $path_on_server, $quiz_post, $quiz_results, $certificate_id, $current_user, $current_time ) {
// Basic check to ensure we have a valid file path and user.
if ( ! empty( $path_on_server ) && file_exists( $path_on_server ) && $current_user instanceof WP_User ) {
// Construct a subject line for the email.
$subject = sprintf(
__( 'Congratulations on completing "%s"!', 'my-text-domain' ),
esc_html( $quiz_post->post_title )
);
// Construct the email body.
$message = sprintf(
__( 'Dear %s, <br><br> We are pleased to inform you that you have successfully completed the quiz "%s" on %s. <br><br> Please find your certificate attached. <br><br> Regards, <br> Your Website Team', 'my-text-domain' ),
$current_user->display_name,
esc_html( $quiz_post->post_title ),
$current_time
);
// Prepare the email headers.
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
// Get the user's email address.
$to = $current_user->user_email;
// Attach the certificate file.
$attachments = array( $path_on_server );
// Send the email. This function requires WordPress core mail functions to be available.
wp_mail( $to, $subject, $message, $headers, $attachments );
// You could also log this event, update user meta, or perform other actions here.
error_log( "Certificate generated for user {$current_user->ID} for quiz {$quiz_post->ID}. Certificate ID: {$certificate_id}." );
}
}
add_action( 'uo_quiz_certificate', 'my_custom_quiz_certificate_handler', 10, 6 ); // 10 is the priority, 6 is the number of accepted arguments.
/**
* Example of how to filter the HTTP URL for the certificate.
* This could be used to add custom query parameters or modify the base URL.
*
* @param string $http_link_to_file The default HTTP URL to the certificate.
* @return string The modified HTTP URL.
*/
function my_custom_certificate_http_url( $http_link_to_file ) {
// Example: Add a timestamp to the URL to force cache busting if needed.
// $modified_url = add_query_arg( 'v', time(), $http_link_to_file );
// return $modified_url;
return $http_link_to_file; // Return the original if no modifications are made.
}
add_filter( 'uo_quiz_certificate_http_url', 'my_custom_certificate_http_url' );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/generate-p-d-f-email.php:166
}
$certificate_id = $certificate_post;
$path_on_server = $file;
$quiz_post = $post;
$quiz_results = $_post['results']['comp'];
$current_time = self::$current_time_stamp;
do_action( 'uo_quiz_certificate', $path_on_server, $quiz_post, $quiz_results, $certificate_id, $current_user, $current_time );
//$http_link_to_file = WP_CONTENT_URL . '/uploads/user-certificates/' . $file_name . '.pdf';
$http_link_to_file = apply_filters( 'uo_quiz_certificate_http_url', WP_CONTENT_URL . '/uploads/user-certificates/' . $file_name . '.pdf' );
do_action( 'uo_quiz_certificate_url', $http_link_to_file, $quiz_post, $quiz_results, $certificate_id, $current_user, $current_time );
$course_id = $setup_parameters['course-id'];