Created
September 14, 2018 16:59
-
-
Save benilla/ec92ce86b9651dc6e59f5712d55fb57b to your computer and use it in GitHub Desktop.
PHP / WORDPRESS - Mailchimp Sub
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
jQuery(document).ready(function($) { | |
function list_sub(form, btn){ | |
input = { | |
action : 'actionSubscribe', | |
group_id : $('.form-subscribe input[name=form-subscribe-type]:checked').val(), | |
fname : form.find('.inputFname').val(), | |
lname : form.find('.inputLname').val(), | |
email : form.find('.inputEmail').val(), | |
} | |
btn.addClass('active'); | |
btn.text('One Moment...'); | |
$.post("/wp-admin/admin-ajax.php", input, function(output){ | |
if( output["success"] == 1){ | |
form.hide(); | |
btn.hide(); | |
$('.modal-subscribe .btn-secondary').show(); | |
$('.modal-subscribe .form-success').show(); | |
} else { | |
form.find('.form-error').html( output["message"] ).show(); | |
} | |
}, 'JSON').fail(function(xhr, desc, err) { | |
console.log( xhr + " " + desc); | |
alert( err ); | |
}); | |
} | |
}); |
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 | |
function mailchimp_subscribe(){ | |
// Ajax | |
$fname = $_POST['fname']; | |
$lname = $_POST['lname']; | |
$email = $_POST['email']; | |
$user_id = $_POST['user_id']; | |
$group_id = $_POST['group_id']; | |
//Vars | |
$list_id = '16e0b3a56a'; | |
$out = array('success' => 0 ); | |
// API | |
$data_center = 'us3'; | |
$api = 'c2e80b596fc40dab093fe96f400f85dd' . '-' . $data_center; | |
$auth = base64_encode( 'user:' . $api ); | |
// Data | |
$data = array( | |
'method' => 'PUT', | |
'headers' => array( | |
'Authorization' => 'Basic ' . $auth | |
), | |
'body' => json_encode(array( | |
'email_address' => $email, | |
'status' => 'subscribed', | |
'merge_fields' => array( | |
'FNAME' => $fname, | |
'LNAME' => $lname | |
), | |
'interests' => array( | |
$group_id => true | |
) | |
)) | |
); | |
$response = wp_remote_post( "https://$data_center.api.mailchimp.com/3.0/lists/$list_id/members/" . md5(strtolower($email)), $data ); | |
$body = json_decode( $response['body'] ); | |
if ( $response['response']['code'] == 200 ) { | |
$out['success'] = 1; | |
$out['response'] = 'The user has been successfully ' . $status . '.'; | |
} else { | |
$out['response'] = '<b>' . $response['response']['code'] . $body->title . ':</b> ' . $body->detail; | |
} | |
//Return | |
echo json_encode($out); | |
wp_die(); | |
} | |
add_action( 'wp_ajax_nopriv_actionSubscribe', 'mailchimp_subscribe' ); | |
add_action( 'wp_ajax_actionSubscribe', 'mailchimp_subscribe' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment