Advertisement
Guest User

Contact Form 7 - Conditional Emails

a guest
Dec 11th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. function conditional_email(&$WPCF7) {
  2.     $url = parse_url($_SERVER['HTTP_REFERER']);         // Get posted page
  3.     $path = ltrim($url['path'], '/');           // Remove first slash
  4.     $path = untrailingslashit($path);           // Remove last slash
  5.     $parts = explode('/', $path);
  6.    
  7.     // Validate parts isn't empty AND is an array AND that array has 2 or more values
  8.     if(!empty($parts) && is_array($parts) && count($parts) > 1)
  9.     {
  10.         if($parts[0] == 'CPT'){         // Check parts to make sure that [0] contains our CPT
  11.             $user = getUser($parts);        // [0] => name [1] => email
  12.                                                    
  13.             if(count($user) == 2)           // Verify we've recieved the 2 indexes correctly, else Bail Out
  14.             {
  15.                 $email = $user[0].' <'.$user[1].'>';    // Format email
  16.                 $WPCF7->mail['recipient'] = $email; // Set Email
  17.             }
  18.         }
  19.     }
  20.   // The Default Email would be set in the Contact Form 7 Form at the Wordpress Backend. Recommended.
  21. }
  22. add_action("wpcf7_before_send_mail", "conditional_email");
  23.  
  24. // Use the path to get the Staff ID then the Staff info
  25. // Returns ARRAY[0] => title [1] => email
  26. function getUser($parts){
  27.     $post = get_page_by_path($parts[1], OBJECT, $parts[0]);     // Get post
  28.     $title = $post->post_title;                 // Get Post Title
  29.     $meta = get_post_meta($post->ID, 'email', true);        // Get value of `email` meta
  30.     $staff_cpt = array($title, $email);
  31.     return $staff_cpt ;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement