0

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: referrer1@example.com. But if someone else shares the link www.example.com/form/referrer2 The form should be emailed to referrer2@example.com

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.

0

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 bob@example.com

  }

  return $recipients;

}
  • It did not work. To check what it did i added some echo's. echo $referer, echo $recipient and echo $recipients[0]. They all worked fine... I got bob bob and bob@example.com. But in some way the plugin keeps only sending the mail to admin@example.com – alarmschijf Aug 14 '16 at 18:15
  • that's weird, if you get correct $referer, $recipient and $recipient[0] then there is no reason for it not to work. or to send it to admin@example.com since we are overwritting $recipient[0] (the default email address). I also just tested here and my form got sent to the correct recipient (based on the referer). So I'm positive that the issue is not with this filter. I am not familiar with formidable (and it would be out of the scope for this thread) but could you have some other setting on your form that is causing this behaviour? I tested with only a text and email field. – bynicolas Aug 14 '16 at 19:37
  • 1
    I must have done something stupid while looking at the plugin fyles. I removed formidable and reinstalled it; and now it works. Thank you very much! – alarmschijf Aug 14 '16 at 20:14

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.