Variables in if statments


Is it possible to use a variable inside an if statement?

Pulling the user’s IP location (2 letter code) using Cloudflare’s IP Geolocation with this inside my fiunctions.php file:

$country_code = $_SERVER[“HTTP_CF_IPCOUNTRY”];


However, not sure how to use this $country_code variable inside the Looper&Logic template.

My goal is to have a custom field for Country with 2-letter Geocodes as values and then to apply an if statement that checks the field=country for the value=“user IP location variable” ← my variable with the user’s 2 letter geo code.

Currently have this set up:

  <Loop type=post taxonomy=products terms="cars">
    <If field field=countries value="{$country_code}">
      <a href="{Field url}"><Field title /></a>


Hey Jack, welcome to the forums! :sunglasses: I saw that you reached out directly earlier today and only just saw your post here, so I thought I’d re-post my response here so that it’s public in case anyone else is asking themselves a similar question.

For starters, it’s important to recognize that PHP variables and L&L variables are not the same thing and can’t natively communicate with each other as you’ve tried to do in the L&L markup you shared. When you declare a variable in your myfunctions.php file, that variable isn’t only accessible within that PHP file unless you do something with it (within your myfunctions.php file) to make it accessible elsewhere. For example, you could theoretically save that variable as a field on the current user’s profile, at which point it would be in the database and L&L would be able to read that user field from your site’s database. Since it sounds like you’re instead trying to create some kind of conditional logic that works even when a user isn’t logged in, the simplest way to do this would probably be to create a shortcode that you could reference throughout your site (including being able to reference it in your L&L code). Here’s an article to get you started with creating a shortcode if you’re not familiar with that.

Once your shortcode has been created, you can then reference the shortcode in your L&L template. Since you’re trying to reference the country code as an attribute value, you’ll probably want to start by using your shortcode to define an L&L variable (again, that’s a different kind of variable than the PHP variable you created earlier) and then use the Get tag to reference the variable in the value of an attribute. So in practice, that could look something like this, depending on how you choose to set up your shortcode and what the rest of your L&L template is supposed to do:

<Set my_loopsandlogic_country_variable>
  <Shortcode my_country_shortcode_name />

<If field=countries value="{Get my_loopsandlogic_country_variable}">
  This text will be displayed when the my_loopsandlogic_country_variable L&L variable matches whatever is contained in the "countries" field for the current page or current loop.

It might also work to use the Shortcode tag directly inside the value attribute like this:

<If field=countries value="{Shortcode my_country_shortcode_name}">
  Content to be displayed goes here.

Although I haven’t played around with that syntax to know if that would work as expected. Worth a try though once you’ve created your shortcode!

I realize this isn’t a complete answer, but I hope the suggestion about defining an L&L variable using a custom shortcode is enough to get you started in tinkering with creating your own conditional logic statements.

Thanks for the in-depth response, I got it working! I will share my NOOB method with any Cloudflare users wanting to do something similar.

End Result: Having a field for ‘country’ with values for countries as 2 letter country codes e.g. US, GB, DE, and to compare these values against a variable that is the user’s IP location dynamically.

For any less techy users or lazy tech pirates, create the geo-ip shortcode quicksmart using “Insert PHP Code Snippet” plugin - add the Cloudflare PHP like this:

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
echo $country_code;

And SAVE, which will look like this (if you make geo your id): [xyz-ips snippet=“geo”]

Then to modify it to work with the Loops&Logic way of working, the shortcode above becomes: <Shortcode xyz-ips snippet="geo" />

The working example as a whole:

<Set my_loopsandlogic_country_variable>
  <Shortcode xyz-ips snippet="geo" />

<If field=countries value="{Get my_loopsandlogic_country_variable}">
  This text will be displayed when the my_loopsandlogic_country_variable L&L variable matches whatever is contained in the "countries" field for the current page or current loop.

Hi Jack, I’ve enjoyed seeing this creative solution.

Passing the country code from Cloudflare, through PHP, shortcode, then to L&L. Quite a trick!

I saw in the PHP documentation for the $_SERVER variable:

…Elements of the $_SERVER array whose keys begin with ‘HTTP_’ come from HTTP request headers and are not to be trusted

To be safe, it’s best to sanitize the value before echo-ing it to the page. Since the final result is used in a tag attribute, I think esc_attr is a suitable function.

The shortcode PHP can be:

echo esc_attr( $_SERVER["HTTP_CF_IPCOUNTRY"] );

The L&L template can be made shorter, by passing the shortcode directly in the tag attribute, as Ben suggested.

<If field=countries value="{Shortcode my_country_shortcode_name}">

Ahh perfect,

Thanks for tightening that up for me, I will improve my L&L with your suggestions.

Great plugin, solved some hoooge headaches.

I asked this in another topic but, what’s the method of checking if my value=“1” is included in an array of field values, so if my post had a custom field with an array of values: 1, 2, 3, 4 - i want to make an If statement that says if field=“X” with my values 1, 2, 3, 4 has value that matches “1”… show these posts.

Currently, I get and set the field to get my 1, 2, 3, 4, then use Check=“1” value="{get and set values 1, 2, 3, 4)".


Looking in the documentation, there’s a section If tag: Common comparisons.

I think includes will do what you described.

<If field=X includes value=1>

The operator in should work too:

<If check=1 in value="{Field X}">