Hi @joshguss,
I’ve had a quick look at 1.0.4. On my local it’s fine, on my test server I get a console error saying:
Uncaught TypeError: Cannot set property 'textContent' of null
Is that what you get? Either way, I think I know why so will have a look at it.
As for your data, I get what you’re trying to do.
You’ll need a little bit more though – one function to store the data and another one to retrieve it for later use, there’s a few ways to do it, but here’s an example using cookies:
First, we hook the success action and save the data (doing a bit of processing too):
add_action('age_gate_form_success', 'ag_success', 10, 2);
function ag_success($data, $errors = [])
{
$dob = intval($data['age_gate_y']). '-' . str_pad(intval($data['age_gate_m']), 2, 0, STR_PAD_LEFT) . '-' . str_pad(intval($data['age_gate_d']), 2, 0, STR_PAD_LEFT);
$from = new DateTime($dob);
$to = new DateTime('today');
$age = $from->diff($to)->y;
$cookieData = json_encode([
'age' => $age,
'region' => $data['ag-region'] ?? false
]);
setcookie('ag_data', $cookieData, 0, '/');
}
Here we calculate the age and save that rather than the full date of birth, but adjust to your needs, other than that, you shouldn’t need to call or do anything with this one.
Then we need another function to get that data and use it:
function ag_user_data()
{
$cookie = isset($_COOKIE['ag_data']) ? stripslashes($_COOKIE['ag_data']) : json_encode(['age' => false, 'region' => false]);
$data = json_decode($cookie);
return $data;
}
Here we just grab that data, or build and empty version if it doesn’t exist.
Then as your example above you can do:
if (ag_user_data()->region === 'ny') {
// New York stuff
} else {
// Other stuff
}
Similarly, you could test and age here:
if (ag_user_data()->age >= 21) {
// Over 21
} else {
// Under 21
}