Country code detection within WordPress and ACF

I have set a code to detect country code within WordPress. How to connect ACF variable and push a value

<?php

require_once ROOT_DIR . "vendor/autoload.php";

use ipinfo\ipinfo\IPinfo;

$user_country_code = $details->country;

if(trim($user_country_code) != "DE") {
$user_outside_germany = true;
$wp->assign("user_outside_germany", $user_outside_germany);
}

?>

Is it wrong line or it can be improved to store value inside ACF variable user_outside_germany?

$wp->assign("user_outside_germany", $user_outside_germany);

What is $wp in this case? It’s not defined in the snippet you posted.

or for that matter, assign(), because i cant find a source that uses that function name in conjunction with WP in general or ACF in specific (though to be frank, ACF’s documentation is… thin at best for an actual coder).

1 Like

$wp->assign(“user_outside_germany”, $user_outside_germany);
$smarty->assign(‘varname’, ‘XXX’);

It is just an example how to define variable inside Smarty framework and I try to use it within WordPress as defined variable name but ACF plugin will be used:
$user_outside_germany

So, defined variable is not needed assign(“user_outside_germany”, $user_outside_germany);

The steps:

  1. ACF plugin and added a Group: Cybersecurity,
  2. ACF plugin Add field and Field Label: User outside Germany and Timestamp
  3. ACF plugin Add Field Name: user_outside_germany and $cur_timestamp
  4. How to store value in the ACF added field name: user_outside_germany?

Can be improved the bottom code?

<?php
require_once ROOT_DIR . "vendor/autoload.php";

use ipinfo\ipinfo\IPinfo;

$user_country_code = $details->country;

if(trim($user_country_code) != "DE") {
$user_outside_germany = true;
}

$cur_timestamp = time();
$cur_timezone_berlin = $cur_timestamp;
$cur_datetime = date("d.n.Y, H:i",$cur_timezone_berlin); // d.n.Y, H:i:s 2024-08-18 13:43:25
$user_outside_germany = get_field('user_outside_germany');

if ($user_outside_germany || $cur_datetime) { ?>
<p>
<strong>User outside Germany:</strong>
<span class="">
<?php echo $cur_datetime ? $cur_datetime . ', ' : ''; ?>
<?php echo $user_outside_germany ? $user_outside_germany . ', ' : ''; ?>
</span>
</p>

<?php } ?>

LMGTFY…
https://www.advancedcustomfields.com/resources/update_field/ACF | update_field() (advancedcustomfields.com)

So, we add PHP code:

$user_outside_germany = get_field('user_outside_germany');
if ($user_outside_germany || $cur_datetime) { ?>
<p>
<strong>User outside Germany:</strong>
<span class="">
<?php echo $cur_datetime ? $cur_datetime . ', ' : ''; ?>
<?php echo $user_outside_germany ? $user_outside_germany . ', ' : ''; ?>
</span>
</p>

<?php } ?>

You probably want a session here rather than ACF as you want one value per visitor (ie some are from Germany some are not) not one value for all visitors (this visitor is from Germany so now all visitors see German time).

So, in your opinion we just do not use ACF in this case and push one visitor if we like.

ACF is for global values, not for values that are about one user. So no, I wouldn’t use it in this case.