wp_mail_content_type
Filters the content type for emails sent by WordPress, allowing customization of email formatting.
add_filter( 'wp_mail_content_type', $callback, 10, 1 );
Description
Filters the content type of emails sent by WordPress. Developers can use this hook to override the default content type (usually 'text/plain' or 'text/html') to send emails in a different format, such as 'text/html' or 'application/json'. This is useful for custom email formatting or sending structured data.
Usage
add_filter( 'wp_mail_content_type', 'your_function_name', 10, 1 );
Parameters
-
$this(mixed) - The `$this` parameter contains the PHPMailer object instance.
Return Value
The filtered value.
Examples
<?php
/**
* Change the default email content type to plain text for specific emails.
*
* This filter is applied to the ContentType property of the PHPMailer object
* before an email is sent. By default, WordPress often uses 'text/html'.
* This example demonstrates how to override it to 'text/plain' if a certain
* condition is met, such as when sending a notification email related to
* a specific plugin functionality.
*/
add_filter( 'wp_mail_content_type', 'my_custom_mail_content_type', 10, 1 );
function my_custom_mail_content_type( $content_type ) {
// In a real-world scenario, you would likely check a global variable,
// a post meta, a user meta, or some other context to determine
// if this specific email should be plain text.
// For this example, let's assume we are sending a password reset email
// and we want to ensure it's delivered as plain text.
// A more robust check might involve inspecting the email recipient,
// subject, or even the content itself, though that's more complex.
// Example: Let's pretend there's a global variable set by another plugin
// or your theme to indicate if this email is a critical notification.
if ( defined( 'MY_CRITICAL_NOTIFICATION_EMAIL' ) && MY_CRITICAL_NOTIFICATION_EMAIL === true ) {
return 'text/plain';
}
// If the condition is not met, return the original content type
// to allow other plugins or WordPress's default behavior to take effect.
return $content_type;
}
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/includes/class.DisableEmailsPHPMailerMock.php:193
src/classes/frontend-login-plus.php:782
if ( $plugin->options['wp_mail_from'] ) {
$this->phpmailer->From = apply_filters( 'wp_mail_from', $this->phpmailer->From );
}
if ( $plugin->options['wp_mail_from_name'] ) {
$this->phpmailer->FromName = apply_filters( 'wp_mail_from_name', $this->phpmailer->FromName );
}
if ( $plugin->options['wp_mail_content_type'] ) {
$this->phpmailer->ContentType = apply_filters( 'wp_mail_content_type', $this->phpmailer->ContentType );
}
if ( $plugin->options['wp_mail_charset'] ) {
$this->phpmailer->CharSet = apply_filters( 'wp_mail_charset', $this->phpmailer->CharSet );
}
if ( $plugin->options['phpmailer_init'] ) {
do_action( 'phpmailer_init', $this->phpmailer );
}