apply_filters( 'wp_mail_content_type', string $content_type )
Filters the wp_mail() content type.
Parameters
-
$content_type
string -
Default wp_mail() content type.
More Information
- The default content type for email sent through the wp_mail() function is ‘
text/plain
‘ which does not allow using HTML. However, you can use thewp_mail_content_type
filter to change the default content type of the email. - In general, content type is going to be ‘
text/plain
‘ as the default, or ‘text/html
‘ for HTML email; but other MIME types are possible.
Source
File: wp-includes/pluggable.php
.
View all references
$content_type = apply_filters( 'wp_mail_content_type', $content_type );
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Switch to HTML formatted email when using
wp_mail()
:Follow the bellow code snippet to set
wp_mail();
content type without usingadd_filter( 'wp_mail_content_type', 'your_function_name' );
.To send HTML formatted mail, you can specify the Content-Type HTTP header in the
$headers
parameter:That’s all about it. Thank you!
Examples migrated from Codex:
The following example will change the default content (mime) type for the
wp_mail()
function to ‘text/html’:The following example shows that it is not necessary to call another method if you can use anonymous functions (PHP 5.3.0+):
The following example shows that you could use different MIME types for different purposes by building some conditional logic into your filter:
Example migrated from Codex:
If you change the content type to `text/html`, it will cause problems with password reset emails.
To remedy this, consider:
modifying the email body of password reset emails to make it work with ‘
text/html
‘ content typeOR
reset the content type back to ‘
text/plain
‘ after you’re done sending the custom emails ( either by explicitly resetting the content type back to ‘text/plain
‘ or by removing the callback function filter that changes the content type to ‘text/html
‘ withremove_filter()
)The following example shows how to use the filter `
retrieve_password_message
` to make the email body of password reset emails work with ‘text/html
‘ content type:The following example shows how to reset the content type back to ‘
text/plain
‘ by removing the callback function filter that changes the content type to ‘text/html
‘ withremove_filter()
: