FWSMC_subform_action_callback

The MailChimp Subscription Plus FWSMC subform action callback function.

Description

FWSMC_subform_action_callback(); 

Usage

  1. if ( !function_exists( 'FWSMC_subform_action_callback' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'mailchimp-subscription-plus/mailchimp-subscription-plus.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = FWSMC_subform_action_callback(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/mailchimp-subscription-plus.php  
  1. function FWSMC_subform_action_callback() { 
  2. $error = ''; 
  3. $status = error; 
  4.  
  5. if (empty($_POST['name']) || empty($_POST['email'])) { 
  6. $error = __( 'Both fields are required to enter.', 'fws-mailchimp-subscribe' ); 
  7. } else { 
  8. if (!wp_verify_nonce($_POST['_fwsmc_subnonce'], 'fwsmc_subform')) { 
  9. $error = __( 'Verification error, try again.', 'fws-mailchimp-subscribe' ); 
  10. } else { 
  11. $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); 
  12. $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); 
  13. $extranamevalue = filter_var($_POST['extramergefield'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); 
  14. $ak = get_option('fwsmc-apiKey'); 
  15. $mc = new \Drewm\MailChimp($ak); 
  16. $fnamevar = get_option('fwsmc-firstNameMergField'); 
  17. $mvars = array('optin_ip'=>$_SERVER['REMOTE_ADDR'], $fnamevar => $name); 
  18. if ($extranamevar = get_option('fwsmc-extraMergeField')) { 
  19. $mvars[$extranamevar] = $extranamevalue; 
  20. $result = $mc->call('lists/subscribe', array( 
  21. 'id' => get_option('fwsmc-listID'),  
  22. 'email' => array('email'=>$email),  
  23. 'merge_vars' => $mvars,  
  24. 'double_optin' => true,  
  25. 'update_existing' => false,  
  26. 'replace_interests' => false,  
  27. 'send_welcome' => false 
  28. )); 
  29.  
  30. //var_dump($result); 
  31.  
  32. if (!empty($result['euid'])) { 
  33. $error = __( 'Thanks, please check your emailbox and confirm your subscription.', 'fws-mailchimp-subscribe' ); 
  34. $status = 'success'; 
  35. } else { 
  36. if (isset($result['status'])) { 
  37. switch ($result['code']) { 
  38. case 214: 
  39. $error = __( 'You\'re already subscribed to this list.', 'fws-mailchimp-subscribe' ); 
  40. break; 
  41. // check the MailChimp API for more options 
  42. default: 
  43. $error = __( 'An unknownerroroccurred.', 'fws-mailchimp-subscribe' ); 
  44. break; 
  45.  
  46.  
  47. $resp = array('status' => $status, 'errmessage' => $error); 
  48. header( "Content-Type: application/json" ); 
  49. echo json_encode($resp); 
  50. die();