Last active
May 4, 2023 17:48
-
-
Save AminulBD/08a223ae288b5b7c5d46 to your computer and use it in GitHub Desktop.
Mailchimp Ajax Subscribe For WordPress
This file contains 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 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
/** | |
* Mailchimp Email Subscription Handler | |
* | |
* @since 1.0.0 | |
* @version 1.0.0 | |
* @author Aminul Islam <[email protected]> | |
*/ | |
function aw_chimp_subscribe() { | |
// MC API and List ID | |
$mc_API = 'Your Mailchimp API key'; | |
$mc_LID = 'Your Mailchimp List IDy'; | |
// Get the Server ID | |
$subscribe_url = 'https://us3.api.mailchimp.com/2.0/lists/subscribe'; | |
// Prepare for MC | |
$email_struct = new StdClass(); | |
$email_struct->email = $_REQUEST['email']; | |
$parameters = array( | |
'apikey' => $mc_API, | |
'id' => $mc_LID, | |
'email' => $email_struct, | |
'double_optin' => false, | |
'send_welcome' => true | |
); | |
// Get data from MC | |
$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); | |
// Run Ajax request | |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
if ( ! empty( $_REQUEST['email'] ) ) { | |
if ( ! empty( $response['error'] ) ) { | |
echo $response['error']; | |
} else { | |
_e( 'Added! Thank you for subscribing.', 'appsworld' ); | |
} | |
} else { | |
_e( 'Please enter your email address.', 'appsworld' ); | |
} | |
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 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(), | |
email: $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 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