Filter uncanny-toolkit-pro

uo_apply_wp_mail_content_type

Filters the WordPress mail content type before it is applied, allowing for customization of email formats.

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

Description

Filters the content type for emails sent by the plugin. Developers can use this hook to modify the email content type, for example, to switch between `text/html` and `text/plain`. This is useful for ensuring compatibility or customizing email display across different email clients.


Usage

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

Return Value

The filtered value.


Examples

<?php
/**
 * Filter the email content type for specific Uo educational plugins.
 *
 * This filter allows developers to conditionally change the WordPress mail
 * content type (e.g., to 'text/html') for emails sent by Uo educational
 * plugins, such as for certificates or group completions.
 *
 * By default, the filter returns true, enabling the change. A developer
 * can return false to prevent the content type from being changed.
 *
 * @param bool $change_content_type Whether to apply the 'wp_mail_content_type' filter.
 * @return bool The modified value of $change_content_type.
 */
add_filter( 'uo_apply_wp_mail_content_type', function( $change_content_type ) {

    // Example: Only change content type for logged-in users.
    if ( is_user_logged_in() ) {
        // You might want to check for specific user roles or capabilities here.
        // For instance, if only administrators should receive HTML emails.
        // if ( current_user_can( 'manage_options' ) ) {
        //     return true; // Allow HTML content type for administrators
        // }

        // For this example, we'll assume any logged-in user should get HTML.
        return true;
    }

    // If not logged in, revert to default (likely text/plain).
    return false;
}, 10, 1 ); // Priority 10, accepts 1 argument.

/**
 * Helper function to set the mail content type to HTML.
 * This function is intended to be used with the 'wp_mail_content_type' filter.
 *
 * @return string The content type.
 */
if ( ! class_exists( 'Uo_Pdf_Email_Generator' ) ) {
    // Simulate the class context if it doesn't exist for demonstration.
    class Uo_Pdf_Email_Generator {
        public static function mail_content_type() {
            return 'text/html';
        }
    }
} else {
    // If the class already exists, use its method.
    // add_filter( 'wp_mail_content_type', array( 'Uo_Pdf_Email_Generator', 'mail_content_type' ) );
}

// The actual logic within the Uo plugins would look something like this:
// (This is for context and not part of the add_filter example itself)
/*
$change_content_type = apply_filters( 'uo_apply_wp_mail_content_type', true );
if ( $change_content_type ) {
    add_filter( 'wp_mail_content_type', array( 'Uo_Pdf_Email_Generator', 'mail_content_type' ) );
}
wp_mail( $to, $subject, $message, $headers );
*/
?>

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:257
src/classes/generate-p-d-f-email.php:281
src/classes/generate-p-d-f-email.php:313
src/classes/generate-p-d-f-email.php:325
src/classes/group-completion-certificate.php:437
src/classes/group-completion-certificate.php:449
src/classes/group-completion-certificate.php:473
src/classes/group-completion-certificate.php:484
src/classes/download-certificates-in-bulk.php:1767
src/classes/download-certificates-in-bulk.php:1845
src/classes/download-certificates-in-bulk.php:1946
src/classes/course-completion-certificate.php:448
src/classes/course-completion-certificate.php:460
src/classes/course-completion-certificate.php:493
src/classes/course-completion-certificate.php:504

$group_sub = str_ireplace( '%User%', html_entity_decode( $user->display_name ), $group_sub );
		$group_sub = str_ireplace( '%User Email%', $user->user_email, $group_sub );
		$group_sub = str_ireplace( '%Group Name%', $ugroups, $group_sub );
		$group_sub = str_ireplace( '%Quiz Name%', $setup_parameters['quiz-name'], $group_sub );
		$group_sub = html_entity_decode( str_ireplace( '$result', $setup_parameters['result'] . '%', $group_sub ) );

		//Sending Email To User!
		$change_content_type = apply_filters( 'uo_apply_wp_mail_content_type', true );
		if ( $change_content_type ) {
			add_filter( 'wp_mail_content_type', array( __CLASS__, 'mail_content_type' ) );
		}

		$email_params['msg'] = do_shortcode( stripslashes( $email_params['msg'] ) );
		$email_param_sub     = $email_params['subject'];
		$email_param_sub     = str_ireplace( '$quizname', $setup_parameters['quiz-name'], $email_param_sub );


Scroll to Top