Forum Email Notifications Contain HTML Code

I am using BBPress for forums. Email notifications are including HTML Code.

1. How do I remove the code from the emails please?

If I click on the link to reply to the post, all looks good on the website with the exception of the <p> tags.

2. How do I remove <p> tags from the actual forum post please?

Please see screenshot of email and then screenshot of forum.
Support Access is granted.

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    That seems to be a plugin or theme conflict. Because by default BBPress, works fine out of the box and display html in replies just fine. You can make a conflict test as described in our getting support manual (flowchart).

    Also, for HTML emails, add following code in functions.php file of your child theme.

    function wpse27856_set_content_type(){
        return "text/html";
    }
    add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

    Please note that, the above code will not remove HTML from emails but instead make that compatible with HTML so email providers can understand the format and display nicely.

    If you still need help then please enable support staff access so I could take a closer look. To grant access go to WPMU DEV -> Support -> Support Access -> Grant Support Access or see this manual for more details regarding how to enable support staff access.

    Take care and happy new year :slight_smile:

    Best Regards,
    Sajid

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    Apologies for delay on this ticket. I logged in to your site and saw that HTML is working the way it should.

    The only problem is that, your member posted the HTML right into Visual mode instead of Text (HTML) mode of the editor.

    So you have to guide your members to post content in the visual mode. All text will automatically be converted and wrapped into P tag of HTML. If they need to add images, then instead of writing img html tag, use Add Media button of the editor (they can insert image via URL too).

    If they really need to add HTML then ask them to switch to Text (HTML) mode of the editor and write/paste html over there (see screenshot).

    Tip: You can teach your users basic WordPress using Integrated Video training videos. It covers all the basics of WP.

    Take care and have a nice day!

    Best Regards,
    Sajid

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    The mu-plugin I gave you in other thread here suppose to remove all html tags from the content before sending an email notification.

    Do you using that or removed it ?

    If you are using and still getting this issue then please send in FTP and WordPress admin details so I could further investigate this issue.

    You can send the requested details via our secure contact form in following format.

    Subject: "Attn: Sajid Javed”
    - WordPress admin username
    - WordPress admin password
    - login url
    - FTP credentials (host/username/password)
    - link back to this thread for reference

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid

  • Sajid

    Hello Mary :slight_smile:

    I see you posted a new thread regarding this issue. Please note the code I gave you to remove the HTML from emails was this one:
    https://premium.wpmudev.org/forums/topic/bbpress-forum-reply-urls-duplicated-in-email-notifications#post-1195649

    In case, you don't want HTML emails then simply remove the code below:

    function wpse27856_set_content_type(){
        return "text/html";
    }
    add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

    and use the above code.

    Also, I remember that one of your user was adding HTML in the visual tab of editor that was being rendered as text instead of tags.

    So please make sure you have could replicate it yourself first, if you get the HTML in your email too then let me know.

    If no then its a human error, to circumvent it, just hide the TEXT/HTML tab of editor. You can do this easily by adding following code in functions.php file of your child theme or use it as mu-plugin.

    function my_editor_settings( $settings ) {
    	$settings['quicktags'] = false;
    	return $settings;
    }
    add_filter('wp_editor_settings', 'my_editor_settings');

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid

  • Mary

    Thank you Sajid for such a complete report. I must say that I'm somewhat confused. You say

    I am thinking that you are saying to remove the code from my functions.php file in child theme (even though it says "Begin - Remove HTML From Emails":wink: and just keep the bp-custom-notifications.php file that is already in mu-plugins.

    I have done this but will have to wait to see if it works.

    Re removing the Text/HTML tab, don't my members need to use that tab if they are copying and pasting? I thought you had to do this or it would put in HTML characters if you pasted into Visual tab. I'm really not sure how it works.

  • Sajid

    Hello Mary :slight_smile:

    I can understand its may be a little confusing because I gave you that code to replace that html with formatting in emails earlier (sorry I did not sated this clearly). That was to tell the email server, how to treat your emails.

    Later on, now you don't want HTML emails at all (just plain text) then you can simply remove that code, since we don't need that.

    Regarding the copy/paste, visual editor should take care of that automatically if its copied and pasted in the visual editor. The problem is when they write html in the editor, so you have to guide your users to not write HTML tags in the visual editor. If they want to format, then simply do it with editor formatting tools.

    Alternatively, they can also paste the content without formatting (command+shift+v for mac and ctrl+shift+v for windows).

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Mary

    I don't want to remove the option of having HTML in my emails. I just don't want to see the actual HTML code. For example I shouldn't be seeing <p> tags, or <p>

    Please can we start over again because I am really confused about what you are asking me to do. Can we start from here as if this is a new support request.

    1. Currently, both bbpress notification emails, and the actual bbpress post, are showing HTML code.

    2. I want to keep the option of being able to use HTML, BUT I don't want the actual HTML code (such as <p> tags) to show

    Please see screenshots.

    Support Access is granted.

  • Mary

    Adding more info to the above.

    I recently received an email that shows <p> tags and   See screenshot.

    Then I added the following code to child theme/functions.php

    //Begin - Forums Remove HTML From Email Notifications
    function wpse27856_set_content_type(){
    return "text/html";
    }
    add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

    I then created another post and received the email notification which has removed the HTML tags, but there is no formatting. I had created three paragraphs but everything is one run-on sentence. See screenshot.

    What I am trying to achieve is for the emails to have the same formatting as was originally posted in the forums - i.e. including paragraphs and breaks, but without HTML tags.

    I hope this explains better what I would like.

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    I can understand the confusion and frustration. After further investigation, I figured it out that it works well when we post a reply as administrator.

    However, while posting as subscriber it does not format correctly. This is because, admin can post unrestricted HTML tags, however, subscribers and participants can only post allowed tags. P tag is not in allowed tags list.

    At the end, I come up with following custom mu-plugin for you to enable HTML tags in emails, enable custom notification emails and allow p tags to subscribers.

    So delete all code I provided you earlier and just download, extract on your computer and upload to wp-content/mu-plugins folder.

    Note: Don't forget to take full backup of your website first.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    I am sorry for delay on this ticket.

    Well, I see in your other thread, Kasia noticed that the issue is resolved and its formatting correctly.

    If it is not resolved then, since I have spent a plenty of time on it already and has better understanding so lets continue in this thread.

    As I mentioned in my last reply, it worked fine on my own test site. If its not working on your site then it must be something setup related, a theme or plugin conflict may be or the way members are posting in forum.

    Any way, to further troubleshoot this issue, I require FTP and direct admin credentials.

    You can send the requested details via our secure contact form in following format.

    Subject: "Attn: Sajid Javed”
    - WordPress admin username
    - WordPress admin password
    - login url
    - FTP credentials (host/username/password)
    - link back to this thread for reference

    Take care and look forward towards your response :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mary :slight_smile:

    Thanks for sending in credentials directly. The problem was that mu-plugin given above working on installed correctly on your site. You uploaded the downloaded plugin as zip archive, where as I mentioned in my last reply that you have to extract the zip file and upload bp-custom-notifications.php in wp-content/mu-plugins folder.

    After, extracting and uploading to mu-plugins folder, it worked as explained above and the way it should.

    Please test again and feel free to let me know if you need assistance with this matter.

    Best Regards,
    Sajid - WPMU DEV Support

    P.S: I created two users, one admin user (sajidwpmudev) to create a topic and another subscriber user (testing) to post a reply on that topic to receive email notifications. Feel free to delete these users.

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    The code changes I made in my last reply were working fine for forum topic reply notifications. That was working fine.

    Now the screenshot is for topic creation notification and comments moderation notification (comments was not part of this discussion).

    Any way, I modified the code accordingly, and tested the topic reply notification, topic creation in forum notification and comments notification (could not test comment on your site site because I did not have access to write post on blog) that worked fine. See below screenshot of all three.


    The following modification I made in the existing file I gave you already (I uploaded the changes on your server too).

    For comments notification formatting:

    add_filter( 'comment_notification_text', 'wpmudev_custom_comment_notification', 10, 2);
    add_filter( 'comment_moderation_text', 'wpmudev_custom_comment_notification', 10, 2);
    
    function wpmudev_custom_comment_notification( $notify_message, $comment_id){
    
        $url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
        $notify_message = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $notify_message);
    
        $notify_message = nl2br($notify_message);
    
        return $notify_message;
    
    }

    For new topic creation notification:

    add_filter( 'bbp_forum_subscription_mail_message', 'wpmudev_topic_custom_message', 10, 3 );
    function wpmudev_topic_custom_message( $message, $topic_id, $forum_id ) {
    
            $topic_content  = bbp_get_topic_content( $topic_id ) ;
            $topic_url      = bbp_get_topic_permalink( $topic_id );
            $topic_author   = bbp_get_topic_author_display_name( $topic_id );
            $forum_name     = bbp_get_forum_title( $forum_id );
    
            $custom_message = '{author} wrote:
    
                                {content}
    
                                Post Link: {url}
    
                                <br />
                                -----------
                                <br /> 
    
                                <p>You are receiving this email because you subscribed to the {forum_name} forum.</p>
    
                                <p>Login and visit the forum to unsubscribe from these emails.</p>';
    
            $message = $custom_message ? $custom_message : $message;
    
            $message = str_replace( '{author}',  $topic_author,  $message );
            $message = str_replace( '{content}', $topic_content, $message );
            $message = str_replace( '{url}',     '<a href="'. $topic_url.'">Read more</a>', $message );
    
            $message = str_replace( '{forum_name}', $forum_name, $message );
    
            return $message;
        }

    Please test the comments on your site and let me know if you need further assistance.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Mary

    Thank you Sajid. I see that these are in the mu-plugins file bp-custom-notifications. I have removed the following from child-theme/functions.php and added it to the bp-custom-notifications. Is this OK?

    // Begin - Forums Remove HTML From Posts
    function bbp_tinymce_paste_plain_text( $plugins = array() ) {
    $plugins[] = 'paste';
    return $plugins;
    }
    add_filter( 'bbp_get_tiny_mce_plugins', 'bbp_tinymce_paste_plain_text' );

  • Sajid

    Hello Mary :slight_smile:

    The code update does not seems to change the recipient address. That just modify the content and add a little bit of formatting.

    Could you please post a reply on this topic with the user account you are getting unformatted email. I subscribe to receive the follow up messages.
    https://christiangays.com/forums/topic/anotherss/

    Also, please tell me the role of that user account you used to post a reply.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    I am sorry for confusion going on here. I know there is problem some where, but the issue is I am unable to replicate it on your site as well as on my own site.

    Could you please share the username and password of the user account you used to create the topic that generated this notification.

    This is because, the user account I created for testing and then creating a new topic in a forum and the email notification generated in result does contain the proper formatting.

    So, I want to see that user role and privileges and see the issue there in order to fix it.

    You can send the username and password using our secure contact form in following format.

    You can send the requested details via our secure contact form in following format.

    Subject: "Attn: Sajid Javed”
    - test username
    - test user password
    - link back to this thread for reference

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello Mary,
    Hope you are doing good today :slight_smile:

    Thanks for sending in the requested credentials. I could see the issue now and to make it clear, it was just because you are using another plugin for notifications along with the custom code I gave you.

    Any way, I edited that plugin code to accept the HTML tags and retain the formatting. I tested on my localhost and it worked there. I could not test on your site since I am not the webmaster there and that can only be sent to webmaster/administrator of the site.

    For record, I edited bbpress-notification.php file of bbPress Notification plugin.

    Replaced this code:
    $topic_content = html_entity_decode(strip_tags(bbp_get_topic_content($topic_id)), ENT_NOQUOTES, 'UTF-8');
    with this:
    $topic_content = html_entity_decode(bbp_get_topic_content($topic_id), ENT_NOQUOTES, 'UTF-8');

    Added of following code to convert URL into links (two instances in this file):

    $url = '~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i';
    $email_body = preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $email_body);

    Replaced this code:
    $reply_content = get_post_field( 'post_content', $reply_id, 'raw' );

    with this one:
    $reply_content = get_post_field( 'post_content', $reply_id, 'display' );

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.