Filter uncanny-toolkit-pro

uo_quiz_completion_add_certificate_attached

Filters whether a certificate should be automatically attached upon quiz completion.

add_filter( 'uo_quiz_completion_add_certificate_attached', $callback, 10, 1 );

Description

Filters whether a certificate file should be attached to the completion email. Return `false` to prevent the attachment from being added. This hook fires after the certificate PDF is generated (or an error occurred) and before the email message is finalized.


Usage

add_filter( 'uo_quiz_completion_add_certificate_attached', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Prevent the certificate from being attached to the email if a specific condition is met.
 * For example, if the quiz is marked as "do not send certificate".
 *
 * @param bool $attach_certificate The default value, true, indicates the certificate should be attached.
 *
 * @return bool False if the certificate should NOT be attached, true otherwise.
 */
add_filter(
	'uo_quiz_completion_add_certificate_attached',
	function ( $attach_certificate ) {
		// Assuming there's a way to check if the current quiz is configured to not send certificates.
		// This is a hypothetical check and would need to be replaced with actual logic from your plugin.
		// For instance, you might be checking post meta, user meta, or a global setting.

		// Example: Let's say we have a custom meta field on the quiz post type like '_do_not_send_certificate'
		// and if it's set to 'yes', we don't want to attach the certificate.
		// This requires access to the current quiz ID, which is not directly passed to this filter.
		// In a real scenario, you might need to get the current quiz ID via global variables or other means.

		// For demonstration, let's simulate a condition where certificate is not attached.
		// You would replace this with your actual conditional logic.
		$should_send_certificate = true; // Default to sending

		// Hypothetical: Get the current quiz ID (you'll need to implement this based on your plugin's context)
		// $current_quiz_id = get_queried_object_id(); // Example for a single quiz page

		// Hypothetical: Check if the quiz has a meta field to disable certificates
		// if ( $current_quiz_id && get_post_meta( $current_quiz_id, '_do_not_send_certificate', true ) === 'yes' ) {
		// 	$should_send_certificate = false;
		// }

		// If the condition is met to NOT send the certificate, return false.
		if ( ! $should_send_certificate ) {
			return false;
		}

		// Otherwise, return the original value (or true, as we are allowing attachment by default).
		return $attach_certificate;
	},
	10, // Priority
	1  // Accepted arguments count
);

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:152

$uo_generate_quiz_certs = apply_filters( 'uo_generate_quiz_certificate', true, $generate_pdf_args, $quiz_id, $current_user->ID );

		if ( $uo_generate_quiz_certs ) {
			$file = self::generate_pdf( $generate_pdf_args );
		}

		if ( apply_filters( 'uo_quiz_completion_add_certificate_attached', true ) ) {
			$email_params['msg'] .= "nn";
			if ( is_array( $file ) && isset( $file['error'] ) ) {
				$email_params['msg'] .= $file['error'];
			} else {
				$email_params['msg'] .= esc_attr__( 'Your certificate is attached with this email.', 'uncanny-pro-toolkit' );
			}
		}


Scroll to Top