uo_quiz_completion_certificate_email_args
Filters the arguments for the quiz completion certificate email, allowing customization before it's sent.
add_filter( 'uo_quiz_completion_certificate_email_args', $callback, 10, 2 );
Description
Filters the arguments for the quiz completion certificate email. Developers can modify the email subject, body, recipient, and other parameters. This hook fires before the certificate email is sent. It's crucial to ensure any modifications maintain the expected structure and data types for the email to be generated and sent correctly.
Usage
add_filter( 'uo_quiz_completion_certificate_email_args', 'your_function_name', 10, 2 );
Parameters
-
$atts(mixed) - This parameter is an empty array that is passed to the filter, intended to prevent default email sending by LearnDash.
-
$pass_args(mixed) - This parameter contains attributes or arguments passed to the function, likely representing configuration or user-specific data related to the quiz.
Return Value
The filtered value.
Examples
add_filter( 'uo_quiz_completion_certificate_email_args', 'my_custom_quiz_certificate_email_args', 10, 3 );
/**
* Customize the arguments passed to the quiz completion certificate email.
*
* This function modifies the email arguments to include additional user information
* and potentially alter the subject or body of the email.
*
* @param array $email_args The default email arguments.
* @param array $atts The original attributes passed to the quiz.
* @param array $pass_args Additional arguments passed along, including $_POST data and time.
* @return array The modified email arguments.
*/
function my_custom_quiz_certificate_email_args( $email_args, $atts, $pass_args ) {
// Ensure we have the necessary data before proceeding.
if ( empty( $pass_args['post'] ) || empty( $pass_args['email_params'] ) ) {
return $email_args;
}
$user_id = get_current_user_id();
$user = wp_get_current_user();
// Add custom recipient information if available in $_POST.
if ( isset( $pass_args['post']['recipient_email'] ) && ! empty( $pass_args['post']['recipient_email'] ) ) {
$email_args['to'] = sanitize_email( $pass_args['post']['recipient_email'] );
} elseif ( ! empty( $user->user_email ) ) {
$email_args['to'] = $user->user_email;
}
// Add custom subject line.
if ( isset( $pass_args['email_params']['quiz_title'] ) && ! empty( $pass_args['email_params']['quiz_title'] ) ) {
$email_args['subject'] = sprintf(
__( 'Congratulations on Completing "%s"!', 'my-text-domain' ),
esc_html( $pass_args['email_params']['quiz_title'] )
);
} else {
$email_args['subject'] = __( 'Quiz Completion Certificate', 'my-text-domain' );
}
// Add custom message body, potentially including user's name and quiz score.
$message_body = '';
if ( ! empty( $user->display_name ) ) {
$message_body .= sprintf( __( 'Dear %s,', 'my-text-domain' ), esc_html( $user->display_name ) ) . "nn";
}
$message_body .= __( 'You have successfully completed the quiz.', 'my-text-domain' ) . "n";
// Example: Add score if available in $_POST or email_params.
$score = '';
if ( isset( $pass_args['post']['quiz_score'] ) && ! empty( $pass_args['post']['quiz_score'] ) ) {
$score = sanitize_text_field( $pass_args['post']['quiz_score'] );
} elseif ( isset( $pass_args['email_params']['score'] ) && ! empty( $pass_args['email_params']['score'] ) ) {
$score = sanitize_text_field( $pass_args['email_params']['score'] );
}
if ( ! empty( $score ) ) {
$message_body .= sprintf( __( 'Your score was: %s', 'my-text-domain' ), $score ) . "n";
}
$message_body .= "n" . __( 'Please find your certificate attached.', 'my-text-domain' );
$email_args['message'] = $message_body;
// You can also add custom headers or modify other email parameters as needed.
// $email_args['headers'] = array( 'X-Custom-Header: MyValue' );
return $email_args;
}
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:430
public static function schedule_generate_quiz_certificate( $atts, $quiz ) {
$pass_args = array(
'email_params' => $atts,
'post' => $_POST,
'time' => time(),
);
//Pass an empty array to the filter so LD doesn't send its emails.
return apply_filters( 'uo_quiz_completion_certificate_email_args', array(), $atts, $pass_args );
}