Renu

Mailchimp Form

Apr 5th, 2012
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. if (function_exists('register_sidebar'))
  5. {
  6. /* original function with 'name' parameter added */
  7. register_sidebar(array(
  8. 'name'=>'right',
  9. 'before_widget' => '',
  10. 'after_widget' => '',
  11. 'before_title' => '',
  12. 'after_title' => ''
  13. ));
  14.  
  15. /* new code copy and pasted from above with name change */
  16. register_sidebar(array(
  17. 'name'=>'left',
  18. 'before_widget' => '',
  19. 'after_widget' => '',
  20. 'before_title' => '',
  21. 'after_title' => ''
  22. ));
  23.  
  24. }
  25.  
  26.  
  27. function createMCForm() {
  28. return '
  29. <div id="formcont">
  30. <form id="myform">
  31.  
  32. <h1>Sign Up to Receive Exclusive <br />
  33. News & Updates
  34. </h1>
  35.  
  36. <fieldset>
  37. <h2>Get the Best Updates</h2>
  38.  
  39. <p class="input">
  40. <span>
  41. <img src="Images/mail_icon.png" class="mail_icon" title="" alt="" />
  42.  
  43. <input type="email" name="EMAIL" class="Email" id="email" />
  44. </span>
  45.  
  46. <input type="hidden" name="action" value="ajax_action" />
  47. <input type="submit" value="" name="subscribe" class="button" id="submit_button" />
  48. </p>
  49.  
  50.  
  51. </fieldset>
  52. </form>
  53. </div>
  54. <div id="output"></div>';
  55. }
  56. add_shortcode('MCform', 'createMCForm');
  57.  
  58.  
  59.  
  60.  
  61. wp_enqueue_script( 'ajax-script', plugin_dir_url(__FILE__).'mailchimp.js', array('jquery'), 1.0 ); // jQuery will be included automatically
  62.  
  63.  
  64. wp_localize_script( 'ajax-script', 'ajax_object', array(
  65. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  66. 'errorEmpty' => __( 'The e-mail field is required' ),
  67. 'errorEmail' => __( 'The entered e-mail address is not valid.' )
  68. )
  69. );
  70.  
  71.  
  72.  
  73.  
  74. add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
  75. add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users
  76.  
  77. function ajax_action_stuff() {
  78. $resp['status'] = 'error';
  79. $resp['errmessage'] = '';
  80. $apikey = 'API KEY REMOVED';
  81. $listID = 'LIST ID REMOVED';
  82. if (!empty($_POST['email']) && !empty($_POST['name'])) {
  83.  
  84. $name = $_POST['name'];
  85. $lname = $_POST['lname'];
  86. $email = $_POST['email'];
  87.  
  88. $url = sprintf('http://api.mailchimp.com/1.2/?method=listSubscribe&apikey=%s&id=%s&email_address=%s&merge_vars[OPTINIP]=%s&output=json&merge_vars[FNAME]=%s&merge_vars[LNAME]=%s', $apikey, $listID, $email, $_SERVER['REMOTE_ADDR'], $name, $lname);
  89. $ch = curl_init($url);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. $data = curl_exec($ch);
  92. curl_close($ch);
  93. $arr = json_decode($data, true);
  94. if ($arr == 1) {
  95. $resp['errmessage'] = 'Check now your e-mail and confirm your subsciption.';
  96. $resp['status'] = 'success';
  97. } else {
  98. switch ($arr['code']) {
  99. case 214:
  100. $resp['errmessage'] = 'You are already subscribed.';
  101. break;
  102. // check the MailChimp API for more options
  103. default:
  104. $resp['errmessage'] = 'Unkown error...';
  105. break;
  106. }
  107. }
  108. } else {
  109. $resp['errmessage'] = 'Missing required fields!';
  110. }
  111. header( "Content-Type: application/json" );
  112. echo json_encode($resp);
  113. exit;
  114. }
  115.  
  116. ?>
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×