-
-
Save jahir07/5066d113159558e7aa1c 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
<form class="newsletter-form top-margin-80 aw-mc-ajax-form" action="<?php echo admin_url( 'admin-ajax.php'); ?>" method="post"> | |
<input name="action" type="hidden" value="aw_chimp_subscribe"> | |
<div class="field-inside"> | |
<i class="fa fa-envelope-o"></i> | |
<input type="text" name="email" class="email-field" placeholder="Type your email address..."> | |
<button type="submit"> | |
<span cLass="subscribe-text"> | |
<?php _e( 'Subscribe', 'appsworld' ); ?> | |
</span> | |
<span cLass="ajax-loader"> | |
<i class="fa-li fa fa-spinner fa-spin"></i> | |
</span> | |
</button> | |
</div><!-- /.field-inside --> | |
<div class="aw-mc-response"></div><!-- /#mailchimp-response --> | |
</form><!-- /.newsletter-form --> |
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
<?php | |
/** | |
* Mailchimp Email Subscription Handler | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* @author Aminul Islam <[email protected]> | |
*/ | |
function aw_chimp_subscribe() { | |
// Mailchimp API and List ID from codestar option panel. | |
$mc_API = 'Your MC API Key'; | |
$mc_LID = 'Your MC List ID'; | |
$srv = substr($settings['mailchimp_api'], -3); | |
$subscribe_url = 'https://' . $srv . '.api.mailchimp.com/2.0/lists/subscribe'; | |
$email_struct = new StdClass(); | |
$email_struct->email = $_REQUEST['mmcemail']; | |
$parameters = array( | |
'apikey' => $mc_API, | |
'id' => $mc_LID, | |
'email' => $email_struct, | |
'double_optin' => false, | |
'send_welcome' => true | |
); | |
$curl = curl_init($subscribe_url); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$response = curl_exec($curl); | |
$response = json_decode($response, true); | |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
if ( ! empty( $response['error'] ) ) { | |
echo $response['error']; | |
} else { | |
echo 'Added! Thank you for subscribing.'; | |
} | |
die(); | |
} | |
else { | |
wp_redirect( home_url() ); | |
exit(); | |
} | |
} | |
// Action it for ajax request | |
add_action( 'wp_ajax_nopriv_aw_chimp_subscribe', 'aw_chimp_subscribe' ); | |
add_action( 'wp_ajax_aw_chimp_subscribe', 'aw_chimp_subscribe' ); |
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
$(".aw-mc-ajax-form").each(function () { | |
var $this = $(this); | |
// Newselleter Scripts | |
$this.submit(function () { | |
$this.find("button[type=\"submit\"]").addClass("clicked"); | |
var action = $(this).attr("action"); | |
$.ajax({ | |
url: action, | |
type: "POST", | |
data: { | |
action: $this.find("input[name=\"action\"]").val(), | |
mmcemail: $this.find("input[name=\"email\"]").val() | |
}, | |
success: function success(data) { | |
$this.find(".aw-mc-response").html(data).addClass("success").css("display", "block"); | |
$this.find("button[type=\"submit\"]").removeClass("clicked"); | |
}, | |
error: function error() { | |
$this.find(".aw-mc-response").html("Sorry, an error occurred.").addClass("error").css("display", "block"); | |
$this.find("button[type=\"submit\"]").removeClass("clicked"); | |
} | |
}); | |
return false; | |
}); | |
}); |
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
<?php | |
// This file to handle form static html file | |
$subscribe_url = "https://us3.api.mailchimp.com/2.0/lists/subscribe"; | |
$email_struct = new StdClass(); | |
$email_struct->email = $_REQUEST['email']; | |
$parameters = array( | |
'apikey' => 'MC_API', | |
'id' => 'MC_LIST', | |
'email' => $email_struct, | |
'double_optin' => false, | |
'send_welcome' => true | |
); | |
$curl = curl_init($subscribe_url); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$response = curl_exec($curl); | |
$response = json_decode($response, true); | |
if (!empty($response['error'])) { | |
echo $response['error']; | |
} else { | |
echo "Thank you for subscribing."; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment