Strip HTML from {{message}}


Home Forums Front End PM PRO Strip HTML from {{message}}

This topic is: Not Resolved
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #45816
    little.chimp
    Participant

    I’m using a custom email template for new messages and I’m attempting to strip the <p></p> that wrap the {{message}} tag.

    I’ve tried…

    In the email template file.
    $message = strip_tags( $message);

    In the email template file.

    function rip_tags($string, $rep = ' ') { 
    
        $string = preg_replace ('/<[^>]*>/', $rep, $string); 
        $string = str_replace("\r", '', $string);    // --- replace with empty space
        $string = str_replace("\n", $rep, $string);   // --- replace with space
        $string = str_replace("\t", $rep, $string);   // --- replace with space
        $string = trim(preg_replace('/ {2,}/', $rep, $string));
        
        return $string; 
    
    }
    
    $message = "{{message}}";
    $message = rip_tags($message);

    function.php

    add_filter( 'fep_filter_before_email_send', function( $content ){
    $content['message'] = strip_tags( $content['message'] );
    return $content;
    }, 99);

    The last one strips all HTML from the email. The first two have no impact.

    Usage <?php echo $message; ?>.

    Any help would be appreciated, I’m sure I’m missing something simple.

    #45817
    little.chimp
    Participant

    $message = strip_tags( $message); should have shown $message = strip_tags('{{message}}');, I mistyped.

    #45818
    little.chimp
    Participant

    Is there a simple way to filter or stop the wpautop on line 116 of pro/includes/class-fep-email-beautify.php, while retaining HTML as the email format?

    #45820
    little.chimp
    Participant

    I’ve edit the code in class-fep-email-beautify.php to allow me to do some testing (I will change it back once I have a solution), which means I’ve been able to pull the message into the email template without the <p></p>.

    But even thought I have {{message}} in a $string, I’m still unable to modify or validate it using any sort of PHP function. It seems immune to my tinkering.

    This lot works as soon as I switch out {{message}} for the actual message copy, until then it comes back ‘false’ even when I know if should be ‘true’.

    function rip_tags($string, $rep = ' ') { 
        
        $string = preg_replace ('/<[^>]*>/', $rep, $string); 
        $string = str_replace("\r", '', $string);    // --- replace with empty space
        $string = str_replace("\n", $rep, $string);   // --- replace with space
        $string = str_replace("\t", $rep, $string);   // --- replace with space
        $string = trim(preg_replace('/ {2,}/', $rep, $string));
        
        return $string; 
    
    }
    
    /* strpos that takes an array of values to match against a string */
    function strpos_arr($haystack, $needle) {
    
    	foreach($needle as $value) {
    		if (strpos($haystack, $value) !== false) {
    			return "<p><b>Warning!</b></p>";
    		}
    	}
    	return false;
    	
    }
    
    $message = "{{message}}";
    $message = rip_tags($message);
    $needle = array('various scam', 'keywords', 'and other stuff');
    $haystack = $message;
    #45821
    little.chimp
    Participant

    I’ve realised I could drop $message = "{{message}}"; from the code and update the “New message content” in the settings to just be {{message}} and still have the message pulled in with just the $message variable. But I’m still unable to modify the $string via PHP functions, plus HTML that’s added to messages is sent through with the email, as I’m unable to strip it out.

    #45825
    little.chimp
    Participant

    I got it all working, I found the reference I need to understand the filter here.

    Here’s the working code.

    function fep_rip_tags($string, $rep = ' ') { 
    
        $string = preg_replace ('/<[^>]*>/', $rep, $string); 
        $string = str_replace("\r", '', $string);    // --- replace with empty space
        $string = str_replace("\n", $rep, $string);   // --- replace with space
        $string = str_replace("\t", $rep, $string);   // --- replace with space
        $string = trim(preg_replace('/ {2,}/', $rep, $string));
        return $string; 
    
    }
    
    function fep_strpos_arr($haystack) {
    	
    	$needle = array('a selection', 'of various', 'keywords', 'related to scams');
    	foreach($needle as $value) {
    		if (strpos($haystack, $value) !== false) {
    			return "<p><b>Warning!</b> This is a scam.</p>";
    		}
    	}
    	return false;
    
    }
    
    add_filter( 'fep_filter_message_before_send', function( $message ) {
    
    	$haystack = $message['message_content'];
    	$message['message_content'] = fep_strpos_arr($haystack) . fep_rip_tags( $message['message_content'] );
    	return $message;
    	
    }, 99);

    It was the $message['message_content']; bit that I was unable to find in the plugin code or docs. But now I know what to look for, it’s everywhere. I’ve return my class-fep-email-beautify.php back to it’s default state too, reenabling wpautop on the copy.

    #45826
    little.chimp
    Participant
    This reply has been marked as private.
    #45827
    Shamim Hasan
    Keymaster

    Do you want to strip only in email (which sent to email address after message sent) or in message as well (which shown in website)?

    #45828
    little.chimp
    Participant

    Originally, I only wanted to strip the HTML from the message of the email, but what I ended up with, striped it from the email and the website. Which worked out as it makes the site more secure, and I was able to modify my attached scam warning message with a bit of CSS, so it made sense on the website and in the email. The code in reply #45825 is simplified compared to the live code, to make it more generic and less specific to my website.

Viewing 9 posts - 1 through 9 (of 9 total)

You need to purchase ‘Front End PM PRO’ to create topic in this support forum.

If you already purchased ‘Front End PM PRO’ please LOGIN.