Created
June 30, 2015 08:13
-
-
Save Sjouw/13a9cc4ed2f05b19cd5c to your computer and use it in GitHub Desktop.
Contact Form 7 (3.8) - change recipient based on zipcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]>"; | |
break; | |
// Recipient 2 | |
case $zipcode >= 1500 && $zipcode <= 1999: | |
$recipient = "Recipient 2 <[email protected]>"; | |
break; | |
// Default recipient | |
default: | |
$recipient = "Recipient default <[email protected]>"; | |
} | |
//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