Last active
August 16, 2022 05:02
-
-
Save gzalinski/1aa1883b43368a859d8b480eb64bedb6 to your computer and use it in GitHub Desktop.
Memberpress Signup - get forms by ajax
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
/** | |
* AJAX Custom Signup | |
*/ | |
add_action('wp_ajax_es_mepr_signup_toggle_form', 'es_mepr_signup_toggle_form_callback'); | |
add_action('wp_ajax_nopriv_es_mepr_signup_toggle_form', 'es_mepr_signup_toggle_form_callback'); | |
function es_mepr_signup_toggle_form_callback() | |
{ | |
try { | |
$plan_id = isset($_REQUEST['plan_id']) && !empty($_REQUEST['plan_id']) ? $_REQUEST['plan_id'] : 0; | |
if (!$plan_id) { | |
throw new Exception('$plan_id is undefined'); | |
} | |
$plan_form = do_shortcode('[mepr-membership-registration-form id="' . $plan_id . '"]'); | |
if ($plan_form) { | |
$response = ['error' => false, 'plan_id' => $plan_id, 'plan_form' => $plan_form]; | |
} else { | |
$response = ['error' => 'Something is wrong !']; | |
} | |
wp_send_json($response); | |
die(); | |
} catch (Exception $error) { | |
wp_send_json(array("error" => $error->getMessage())); | |
die(); | |
} | |
} |
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
function toggle_signup_form(signupContent, signupHeader, toggle, toggle_input) { | |
const checked = !toggle_input.prop('checked') | |
const plan = toggle_input.data('plan') | |
const plan_id = checked ? plan.yearly.plan_id : plan.monthly.plan_id; | |
const plan_price = checked ? plan.yearly.price : plan.monthly.price; | |
const price = signupHeader.find('.es-signup__price') | |
$.ajax({ | |
url: misfit.ajaxurl, | |
type: 'POST', | |
cache: 'false', | |
data: { | |
'action': 'es_mepr_signup_toggle_form', | |
'plan_id': plan_id, | |
}, | |
beforeSend: function (response) { | |
signupHeader.addClass('disabled') | |
signupContent.addClass('disabled') | |
toggle.addClass('loading') | |
toggle_input.prop('checked', checked) | |
}, | |
success: function (response) { | |
if (response.error) { | |
alert(response.error); | |
} else { | |
signupContent.html(response?.plan_form) | |
price.html(`$${plan_price} / month`) | |
signupHeader.removeClass('disabled') | |
signupContent.removeClass('disabled') | |
toggle.removeClass('loading') | |
} | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment