Home › Forums › Front End PM PRO › Strip HTML from {{message}}
Tagged: Email Template, message, Strip HTML
- This topic has 8 replies, 2 voices, and was last updated 1 year ago by little.chimp.
-
AuthorPosts
-
December 7, 2023 at 7:30 pm #45816little.chimpParticipant
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.
December 7, 2023 at 7:33 pm #45817little.chimpParticipant$message = strip_tags( $message);
should have shown$message = strip_tags('{{message}}');
, I mistyped.December 7, 2023 at 8:44 pm #45818little.chimpParticipantIs 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?
December 7, 2023 at 11:07 pm #45820little.chimpParticipantI’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;
December 7, 2023 at 11:59 pm #45821little.chimpParticipantI’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.December 8, 2023 at 3:12 pm #45825little.chimpParticipantI 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.December 8, 2023 at 3:15 pm #45826little.chimpParticipantThis reply has been marked as private.December 10, 2023 at 3:29 pm #45827Shamim HasanKeymasterDo you want to strip only in email (which sent to email address after message sent) or in message as well (which shown in website)?
December 10, 2023 at 7:02 pm #45828little.chimpParticipantOriginally, 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.
-
AuthorPosts
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.