Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (function_exists('register_sidebar'))
- {
- /* original function with 'name' parameter added */
- register_sidebar(array(
- 'name'=>'right',
- 'before_widget' => '',
- 'after_widget' => '',
- 'before_title' => '',
- 'after_title' => ''
- ));
- /* new code copy and pasted from above with name change */
- register_sidebar(array(
- 'name'=>'left',
- 'before_widget' => '',
- 'after_widget' => '',
- 'before_title' => '',
- 'after_title' => ''
- ));
- function createMCForm() {
- return '
- <div id="formcont">
- <form id="myform">
- <h1>Sign Up to Receive Exclusive <br />
- News & Updates
- </h1>
- <fieldset>
- <h2>Get the Best Updates</h2>
- <p class="input">
- <span>
- <img src="Images/mail_icon.png" class="mail_icon" title="" alt="" />
- <input type="email" name="EMAIL" class="Email" id="email" />
- </span>
- <input type="hidden" name="action" value="ajax_action" />
- <input type="submit" value="" name="subscribe" class="button" id="submit_button" />
- </p>
- </fieldset>
- </form>
- </div>
- <div id="output"></div>';
- }
- add_shortcode('MCform', 'createMCForm');
- wp_enqueue_script( 'ajax-script', plugin_dir_url(__FILE__).'mailchimp.js', array('jquery'), 1.0 ); // jQuery will be included automatically
- wp_localize_script( 'ajax-script', 'ajax_object', array(
- 'ajaxurl' => admin_url( 'admin-ajax.php' ),
- 'errorEmpty' => __( 'The e-mail field is required' ),
- 'errorEmail' => __( 'The entered e-mail address is not valid.' )
- )
- );
- add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
- add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users
- function ajax_action_stuff() {
- $resp['status'] = 'error';
- $resp['errmessage'] = '';
- $apikey = 'API KEY REMOVED';
- $listID = 'LIST ID REMOVED';
- if (!empty($_POST['email']) && !empty($_POST['name'])) {
- $name = $_POST['name'];
- $lname = $_POST['lname'];
- $email = $_POST['email'];
- $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);
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- $arr = json_decode($data, true);
- if ($arr == 1) {
- $resp['errmessage'] = 'Check now your e-mail and confirm your subsciption.';
- $resp['status'] = 'success';
- } else {
- switch ($arr['code']) {
- case 214:
- $resp['errmessage'] = 'You are already subscribed.';
- break;
- // check the MailChimp API for more options
- default:
- $resp['errmessage'] = 'Unkown error...';
- break;
- }
- }
- } else {
- $resp['errmessage'] = 'Missing required fields!';
- }
- header( "Content-Type: application/json" );
- echo json_encode($resp);
- exit;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement