Br not working in wp_mail
The br
tag not working in wp_mail
can be a frustrating issue!
The wp_mail
function in WordPress uses the wp_mail_content_type
filter to determine the content type of the email. By default, this filter is set to text/plain
, which means that any HTML tags, including br
, will be stripped from the email content.
To fix this issue, you can try one of the following solutions:
- Use the
wp_mail_content_type
filter: You can use thewp_mail_content_type
filter to set the content type totext/html
, which will allow HTML tags, includingbr
, to be rendered in the email. You can do this by adding the following code to your theme'sfunctions.php
file:function set_wp_mail_content_type_to_html($content_type) { return 'text/html'; } add_filter('wp_mail_content_type', 'set_wp_mail_content_type_to_html');
- Use the
wp_mail_charset
filter: Another option is to use thewp_mail_charset
filter to set the charset of the email to a value that supports HTML entities, such as UTF-8. You can do this by adding the following code to your theme'sfunctions.php
file:function set_wp_mail_charset_to_utf8($charset) { return 'UTF-8'; } add_filter('wp_mail_charset', 'set_wp_mail_charset_to_utf8');
- Use the
wp_mail
function with theformat
parameter: You can also use thewp_mail
function with theformat
parameter set toHTML
. This will allow you to send HTML emails withbr
tags intact. For example:wp_mail($to, $subject, $message, 'Content-Type: text/html; charset=UTF-8');
Note that the
format
parameter is only available in WordPress 4.4 and later versions.
I hope one of these solutions works for you!