Filter uncanny-toolkit-pro

uo_course_completion_add_certificate_attached

Filters whether a completion certificate is attached when a course is marked as complete.

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

Description

This filter allows developers to control whether a certificate is automatically attached to course completion emails. By default, it returns `true`, enabling the attachment. Return `false` to prevent the certificate from being attached.


Usage

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

Return Value

The filtered value.


Examples

add_filter( 'uo_course_completion_add_certificate_attached', 'my_custom_certificate_attached_message', 10, 1 );

/**
 * Custom function to modify the message indicating a certificate is attached.
 *
 * This example shows how to conditionally alter the default message based on
 * whether the certificate is actually available or if a specific condition is met.
 *
 * @param bool $attached_by_default Whether the certificate is considered attached by default.
 * @return string The modified message to be appended to the email.
 */
function my_custom_certificate_attached_message( $attached_by_default ) {
    // In a real scenario, you might check other conditions here.
    // For example, if the certificate generation failed for some reason,
    // you might want to return an empty string or a different message.
    // We'll assume for this example that if the default is true, we want to
    // append our custom message.

    if ( $attached_by_default ) {
        // Check if a specific user meta indicates they prefer not to receive the attachment message.
        // This is a hypothetical example. You'd adapt this to your actual needs.
        $current_user_id = get_current_user_id();
        $prefers_no_attachment_message = get_user_meta( $current_user_id, 'prefers_no_certificate_message', true );

        if ( ! empty( $prefers_no_attachment_message ) && $prefers_no_attachment_message === 'yes' ) {
            return ''; // Don't add any message if the user prefers.
        }

        // Return a custom message with an added benefit or reminder.
        return esc_html__( 'Great news! Your achievement certificate is attached. Please download and save it.', 'your-text-domain' );
    }

    // If the default is false, return an empty string or a specific message for when it's not attached.
    return '';
}

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/course-completion-certificate.php:411

$email_message = str_ireplace( '%Course Name%', $setup_parameters['course-name'], $email_message );
		$email_message = str_ireplace( '%Group Name%', $ugroups, $email_message );

		$email_message .= "nn";

		if ( is_array( $file ) && isset( $file['error'] ) ) {
			$email_message .= $file['error'];
		} elseif ( apply_filters( 'uo_course_completion_add_certificate_attached', true ) ) {
			/** Commented out so we have full control of message */
			$email_message_certificate_attached_text = apply_filters( 'uo_course_certificate_email_message_certificate_attached_text', esc_attr__( 'Your certificate is attached with this email.', 'uncanny-pro-toolkit' ));
			$email_message .= $email_message_certificate_attached_text;
		}

		$non_user_body = str_ireplace( '%User%', html_entity_decode( $user->display_name ), $non_user_body );
		$non_user_body = str_ireplace( '%User First Name%', html_entity_decode( $user->first_name ), $non_user_body );


Scroll to Top