0

I'm trying to alter the content of an email generated by a plugin, removing a lengthy section that starts with <p> NOTE: (the space is actually a tab) and ending with /</a><br /> (lines 2-5 below). There are all sorts of characters and line feeds in between. I can't get the search to work. Here is the type of content and what I've tried at PHP Sandbox. I thought a non-greedy quantifier would work, but no joy.

$content = 'Weather: rainy</p>
<p> NOTE: You are entering the twilight zone:<br />
 &#8211; No cell phones</p>
<p>Click here:<br />
<a href="https://www.example.com/" rel="nofollow">https://www.example.com/</a><br />
&#8212;<br />
To exit, visit:<br />
<a href="https://example.org/wp-admin/admin.php?page=twilight;subpage=global_options" rel="nofollow">https://example.org/wp-admin/admin.php?page=twilight;subpage=global_options</a><br />
To see current twilight zone conditions, visit:<br />';

$search = '~<p>\sNOTE[.\n\r]+?/</a><br />~';
$replace = '';
$content = preg_replace($search, $replace, $content);
echo $content;
6
  • is the text always the same? This looks like a generic PHP question not a WordPress question, and preg_replace isn't a WordPress function it's a PHP core function. I'd suggest asking on stackoverflow, but they might respond by saying you should use an XMLParser instead of regular expressions as Regex only works for very simply HTML and can't fully parse HTML due to its structure. Have you contacted the plugin author? There's probably a hook you can remove
    – Tom J Nowell
    Dec 10 '20 at 17:11
  • I think, yes, the text is always the same, though not as shown for example. I finally got it to work with this search code '~<p>\sNOTE(.|\s)+?/</a><br />~';, though I don't understand why the original didn't work. Dec 10 '20 at 17:57
  • Oops, correction. It worked in PHP Sandbox (php 7.4.0), but not on the actual website (7.4.13). Another preg_replace change I'm making to the same email does work on the site. Dec 10 '20 at 18:16
  • So I think now it is a Wordpress issue since it works in pure php but not in Wordpress. Dec 10 '20 at 18:27
  • Replacing '~\tNOTE(.|\s)+?/</a><br />~' with nothing works as expected in PHP Sandbox, but it deletes the entire email content in Wordpress. Is it possible the email content when the filter is acting (mailtpl/email_content from the Email Templates plugin) is different from the raw source content of the received email? Dec 10 '20 at 21:16
1

I finally got it figured out. In order to see the content that I was trying to edit, I turned on debugging in wp-config.php:

define('WP_DEBUG', true); // set true
if ( WP_DEBUG ) {
    define('WP_DEBUG_LOG', '../logs/debug.log' );
    define('WP_DEBUG_DISPLAY', false);  // don't display errors on pages
    @ini_set( 'display_errors', 0 );
}

Then, in the function that does the replacing, I commented out that stuff and instead just put error_log( print_r( $content, true ) );. When I triggered the email, the $content then showed up in that log.

Turns out the content IS different when the filter is applied than in the final email raw source. The $content has no html except for <a> tags for links. Once I changed the search pattern to match what was there in the log, it worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.