Last active
April 4, 2017 04:26
-
-
Save ossvn/eaab5996c2aeb697bda862eed1adf70d to your computer and use it in GitHub Desktop.
ACF Form Builder - Register Custom Action - Do your own thing after user press submit button
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 | |
/** | |
* Subscribe via Mailchimp | |
*/ | |
//Your function name must start with acf_fb_custom_actions_ | |
function acf_fb_custom_actions_subscribe_via_mailchimp($data) { | |
//We already had $data - you can var_dump() to see what is inside | |
if ( is_null($data) ) | |
return; | |
/* Prepre the form settings */ | |
$form_id = $data['group_id']; | |
$group_meta = get_post_meta( $form_id ); | |
$form_settings = unserialize($group_meta['form_settings'][0]); | |
/* Prepare fields data */ | |
if (isset($data['acf'])) // acf pro version | |
$fields = $data['acf']; | |
else // acf free version | |
$fields = $data['fields']; | |
$fields_object = array(); | |
foreach ($fields as $key => $field) { | |
$fields_object[$key] = get_field_object($key); | |
} | |
$email_subscribe = array(); | |
foreach ($fields_object as $key => $value) { | |
$email_subscribe[$key]['name'] = $value['name']; | |
$email_subscribe[$key]['field_type'] = $value['type']; | |
$email_subscribe[$key]['value'] = $fields[$key]; | |
$email_subscribe[$key]['form_field_type'] = $value['form_field_type']; | |
} | |
$prepare = array(); | |
foreach ($email_subscribe as $key => $value) { | |
if ('subscribe_email'==$value['form_field_type']) $prepare['subscribe_email'] = $value['value']; | |
if ('custom'==$value['form_field_type']) $prepare['custom'][$key] = $value['value']; | |
} | |
/* Get Mailchimp APIs */ | |
$mailchimp_api = $form_settings['mailchimp_api']; | |
$mailchimp_list_id = $form_settings['mailchimp_list_id']; | |
/* Almost there */ | |
$status = 'subscribed'; // subscribed, cleaned, pending | |
$args = array( | |
'method' => 'PUT', | |
'headers' => array( | |
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key ) | |
), | |
'body' => json_encode(array( | |
'email_address' => $prepare['subscribe_email'], | |
'status' => $status | |
)) | |
); | |
foreach ($prepare['subscribe_email'] as $email_subscribe_post => $email_adress) { | |
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $mailchimp_list_id . '/members/' . md5(strtolower($email_adress)), $args ); | |
} | |
wp_redirect('https://codecanyon.net/item/acf-form-builder-multipurpose-frontend-form-submission/19611604', 301); | |
exit; | |
} | |
add_action( 'acf_fb_custom_actions', 'acf_fb_custom_actions_subscribe_via_mailchimp' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment