Last active
May 20, 2016 20:16
-
-
Save aabir/e806c9bae2174ee6cb2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<div style="background: rgba(0,0,0,0.4); border: 2px solid #fff; padding: 20px 15px; margin: 50px 0; border-radius: 5px; "> | |
<h3 style="color: #fff; text-align: center; margin-bottom: 10px;"> Newsletter </h3> | |
<script> | |
jQuery(document).ready(function($) { | |
$("#subscribe2").click(function(){ | |
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); | |
var name=$('#name2').val(); | |
var email=$('#email2').val(); | |
var listId=''; //list ID | |
if(pattern.test(email) && name) | |
{ | |
$.ajax({url:"http://www.submit_url.com/wp-admin/admin-ajax.php",type:"post",data:"action=send_newsletter&name="+name+"&email="+email+"&listId="+listId,success:function(result){ | |
result = result.replace("0", ""); | |
if(result=='ok') | |
{ | |
result='Thank you for subscribing to our newsletter.'; | |
$('#result2').css('color','#fff'); | |
} | |
else | |
{ | |
result=email+' is already subscribed to our newsletter list.'; | |
$('#result2').css('color','red'); | |
} | |
$('#result2').html(result); | |
}}); | |
} | |
}); | |
}); | |
</script> | |
<p id="result2" style="text-align:center"></p> | |
<form id="newsletter" action="javascript:void(0)"> | |
<input type="text" name="newsletter_name" id="name2" class="newsletter_name" placeholder="Your Name" required="required" /> | |
<input type="email" name="newsletter_email" id="email2" class="newsletter_email" placeholder="Your Email" required="required" /> | |
<input type="submit" class="newsletter btn" id="subscribe2" value="SUBSCRIBE" /> | |
</form> | |
</div> |
This file contains hidden or 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
/* Mail Chimp API */ | |
add_action( 'wp_ajax_nopriv_send_newsletter', 'send_newsletter' ); | |
add_action( 'wp_ajax_send_newsletter', 'send_newsletter' ); | |
function send_newsletter() | |
{ | |
$email=$_POST['email']; | |
$name=$_POST['name']; | |
if($name && $email) | |
{ | |
$submit_url = "https://us11.api.mailchimp.com/2.0/lists/subscribe.json"; //Submit URL | |
$send_data=array( | |
'email'=>array('email'=> $email), | |
'apikey'=>"", //API key | |
'id'=>"a482308945", | |
'merge_vars'=> array('FNAME'=>$name), | |
'double_optin'=>false, | |
'update_existing'=>false, | |
'replace_interests'=>false, | |
'send_welcome'=>false, | |
'email_type'=>"html", | |
); | |
$payload = json_encode($send_data); | |
$ch=curl_init(); | |
curl_setopt($ch,CURLOPT_URL,$submit_url); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($ch,CURLOPT_POST,true); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$payload); | |
$result=curl_exec($ch); | |
curl_close($ch); | |
$data = json_decode($result); | |
if (isset($data->error)){ | |
echo $data->error; | |
} else { | |
echo "ok"; | |
} | |
} | |
else | |
echo 'Error!!!!'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment