I know it’s not the best way, but made it work like this:
JS
document.querySelector('.age-gate__submit--yes').addEventListener('click', async (e) => {
const header = {
'accept': 'application/json',
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*'
};
const email = document.getElementsByClassName("age-email-input")[0].value;
const endpointUrl = https://yoursite.com/wp-json/yoursite/age-gate-email;
const rawResponse = fetch(endpointUrl, {
method: 'POST',
headers: header,
body: JSON.stringify({email: email })
});
});
Theme Function.php
function ageGateAddUser( WP_REST_Request $request ) {
$parameters = $request->get_json_params();
$email = $parameters['email'];
if($email!=""){
//Do whatever you need here
}
return new WP_REST_Response($email);
}
add_action( 'rest_api_init', function () {
register_rest_route( 'yoursite', 'age-gate-email', array(
'methods' => 'POST',
'callback' => 'ageGateAddUser',
));
});
Plugin Author
Phil
(@philsbury)
Hi @lickybuay,
Sorry for the delay. There’s a guide for doing exactly this in the docs (one of the few things that is actually filled out fully!)
You can find it here: https://agegate.io/docs/v3/extending-age-gate/adding-custom-form-fields/advanced-example
To store it, you’d use the age_gate/submission/success action. That one does need updating in the docs, but it takes three params $data is general age gate stuff, $errors (which will be an empty array as it’s successful) and $custom which is where you’d find and custom field data you have posted. Works in both PHP and JS modes of the plugin
Thanks
Phil
Exo
(@richardshea)
This answer seems very vague to me, I’m needing the same solution – a simple JS hook or call I can make to confirm if the Age Gate has been as success.
Does anyone have some code they can share?
A real shame the documentation is incomplete, I would have thought the need for a hook was more common if a site has other popups or interaction once Age Gate is done?
Thanks in advance.
Exo
(@richardshea)
Found another thread, for those landing here this works:-
window.addEventListener(‘agegatepassed’, function () {
// your call here
})