Skip to content

Instantly share code, notes, and snippets.

@Sjouw
Created June 30, 2015 08:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sjouw/13a9cc4ed2f05b19cd5c to your computer and use it in GitHub Desktop.
Save Sjouw/13a9cc4ed2f05b19cd5c to your computer and use it in GitHub Desktop.
Contact Form 7 (3.8) - change recipient based on zipcode
<?php
/*
Plugin Name: Contact Form 7 Zipcode check
Plugin URI: http://youruri.com/
Description:
Author:
Version: 1.0
Author URI:
*/
function prefix_zipcode_check (&$WPCF7_ContactForm) {
$form_id = $WPCF7_ContactForm->id;
// Check if it is the correct form
if ($form_id === 1) {
// Replace ['zipcode'] with the correct fieldname
$zipcode = $WPCF7_ContactForm->posted_data['zipcode'];
// Remove all characters other than numbers
$zipcode = preg_replace("/[^0-9]/", "", $zipcode);
//Access the correct property, which is form in this case
$recipient = $WPCF7_ContactForm->mail['recipient'];
//Sets the correct recipient value (e-mail adress) for each ZIP code range
switch($zipcode) {
// Recipient 1
case $zipcode >= 1000 && $zipcode <= 1499:
$recipient = "Recipient 1 <recipient1@domain.com>";
break;
// Recipient 2
case $zipcode >= 1500 && $zipcode <= 1999:
$recipient = "Recipient 2 <recipient2@domain.com>";
break;
// Default recipient
default:
$recipient = "Recipient default <recipientdefault@domain.com>";
}
//Sets the property for the recipient with the variable defined in the switch statement above
$WPCF7_ContactForm->mail['recipient'] = $recipient;
}
}
add_action("wpcf7_before_send_mail", "prefix_zipcode_check");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment