---
title: Intercepting WebForm Tests
date: 2025-01-17T23:17:36Z
modified: 2026-09-22T19:51:44Z
permalink: "https://techarticles.co.uk/intercepting-webform-tests/"
type: post
status: publish
excerpt: ""
wpid: 4340
categories:
  - Automation
  - Blog
  - Coding
  - Wordpress
timestamp: 2026-09-22T19:51:44Z
tags:
  - Automation
  - Blog
  - Coding
  - Wordpress
---

Following on from my testing web forms videos released earlier this week, I received a follow-up question

The question was, how do I know it worked and delivered? Now, for me, this isn’t an issue. They end up in my email box, and that’s all I need to worry about, but how would I approach this as a host or developer?

I would intercept the email using a known phrase in this example, we use “WebForm Testing X5854” unless your client is a web form tester it’s very unlikely this phrase would be in the email for any legitimate reason, and as such, we look and filter emails with that within the body and if that matches we use the wp\_mail filter to change the recipient to our agency email address

The snippet below is all you need; change the intercept string (I’d recommend a unique one for each client) and set the snippet in your config plugin or functions.php or snippet plugin, and away you go.

```
<span class="line"><span style="color: #F97583"><?</span><span style="color: #79B8FF">php</span><span style="color: #E1E4E8"> </span></span>
<span class="line"><span style="color: #F97583">function</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">intercept_email_and_modify_recipient</span><span style="color: #E1E4E8">( $args ) {</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #6A737D">// Define the string to search for in the email content</span></span>
<span class="line"><span style="color: #E1E4E8">    $string_to_intercept </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">'WebForm Testing X5854'</span><span style="color: #E1E4E8">; </span><span style="color: #6A737D">// Change to what you actually are looking for as random as possible!!!</span></span>
<span class="line"><span style="color: #E1E4E8">    $new_email_address   </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">'you@youragency.com'</span><span style="color: #E1E4E8">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #6A737D">// Check if the email body contains the specific string</span></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">if</span><span style="color: #E1E4E8"> ( </span><span style="color: #79B8FF">strpos</span><span style="color: #E1E4E8">( $args[</span><span style="color: #9ECBFF">'message'</span><span style="color: #E1E4E8">], $string_to_intercept ) </span><span style="color: #F97583">!==</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">false</span><span style="color: #E1E4E8"> ) {</span></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #79B8FF">error_log</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">'Email intercepted: Changing recipient to '</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">.</span><span style="color: #E1E4E8"> $new_email_address);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">        </span><span style="color: #6A737D">// Change the recipient to the new email address</span></span>
<span class="line"><span style="color: #E1E4E8">        $args[</span><span style="color: #9ECBFF">'to'</span><span style="color: #E1E4E8">] </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> $new_email_address;</span></span>
<span class="line"><span style="color: #E1E4E8">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">    </span><span style="color: #F97583">return</span><span style="color: #E1E4E8"> $args;</span></span>
<span class="line"><span style="color: #E1E4E8">}</span></span>
<span class="line"><span style="color: #B392F0">add_filter</span><span style="color: #E1E4E8">( </span><span style="color: #9ECBFF">'wp_mail'</span><span style="color: #E1E4E8">, </span><span style="color: #9ECBFF">'intercept_email_and_modify_recipient'</span><span style="color: #E1E4E8"> );</span></span>
<span class="line"></span>
```PHP

You can watch the follow-up video to see this in action here

![](https://i.ytimg.com/vi/gqzwcCnJ8ak/maxresdefault.jpg)