Send contact form to a specific referrer-email adres

My objective is to have a form that can be send to different email adresses, depending on the referrer.

For example: If someone would share this link: www.example.com/form/referrer1 The form should be emailed to: [email protected]. But if someone else shares the link www.example.com/form/referrer2 The form should be emailed to [email protected]

Can this be done using just one form. Instead of a new form for every user. If so, how? Can this be done with the formidable plugin, which I currently use.

Hope someone can help. Thank you in advance.

Topic email references forms Wordpress

Category Web


I think this can be achieved.

My first thought was just to use endpoints and modify the recipient email based on the query string. But looking at formidable hooks, the only one I see suitable for this is the frm_to_email filter. which is applied just after send (where our query string are not longer available).

But we can still work around that issue by using the $_POST variable and it's _wp_http_referer key.

We would still need to add an endpoint to WP, if not we would get 404 errors because WP wouldn't know what to do with the dynamic URL.

Assuming the following,

  • Your form is on a page called form
  • We will use an endpoint referer to identify the incoming request so we would have a relative URL looking like /form/referer/bob, where bob is the variable part of the incoming URL

so first let's add our endpoint

add_action( 'init', 'wpse_235869_add_endpoint');
function wpse_235869_add_endpoint() {
  add_rewrite_endpoint( 'referer', EP_PAGES ); // EP_PAGES is mask telling to listen for the referer endpoint on all PAGES
}

this added an endpoint which would translate in a query string like /form?referer=bob

Then we need to tell WP to listen for our new query string referer

add_filter( 'query_vars', 'wpse_235869_add_queryvars' );
function wpse_235869_add_queryvars( $query_vars ) {
  $query_vars[] = 'referer';
  return $query_vars;
} 

Once this is done, we can move on to write our formidable form filter.

Upon sending of the form, a $_POST variable is created with a our _wp_http_referer key, which, in this example, would be [_wp_http_referer] => /form/referer/bob/

so our formidable filter would look something like this

add_filter('frm_to_email', 'custom_set_email_value', 10, 4);
function custom_set_email_value($recipients, $values, $form_id, $args){

  if( isset($_POST['_wp_http_referer']) ) { 
    preg_match( '#/form/referer/#', $_POST['_wp_http_referer'], $matches );  // check for our endpoint pattern so we don't try to modify recipients on regular forms.
    if ( $matches[0] != '/form/referer/') // If the _wp_http_referer does not match our endpoint, bail out.
      return $recipients;

    $referer = explode( '/form/referer', $_POST['_wp_http_referer'] )[1]; // get our recipient from the referer.
    $recipient = str_replace( '/', '' , $referer ); // remove forward slashes from $rerefer so we can use in email address.


    $recipients[0] = $recipient . '@example.com'; // rebuild our recipients array before sending. This, in our example, would be [email protected]

  }

  return $recipients;

}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.