If you are one of the thousands of WordPress developers that like me use Advanced Custom Fields plugin to easily create backends for your designs you will find today’s tutorial very interesting. If you don’t use ACF yet, you don’t know what you are missing!
Adding Geotargeting to ACF
Today I will explain how you can add geolocation to ACF using the Geotargeting plugin API. Once you got both plugins installed the first thing you need to do is to create a new field group and start adding fields that later you will code in the frontend to show amazing results.
The Geotargeting field will be added in the basic field type for both ACFv4 and ACFv5 plugins.
Once you choose Geotargeting field type it will let you choose between countries or regions. Just choose whatever you need to work with, I mostyl use regions for my clients.
Backend with ACF and Geotargeting
On this example I will use a repeater field as is most probably that you will use repeater fields to geotarget different fields or content. On this example inside the repeater I got three fields, the geotargeting field, a radio field and a text field as shown in the picture below
What I’m doing on this example is showing the google url to USA region ( you can specify regions on the plugin settings page) and Timersys url to everyone else that is not from that region. So far everything it’s visual and easy , now let’s see how this looks in our code.
ACF and Geotargeting in PHP
To finish we simple need to combine ACF and Geotargeting API functions to get desired result:
This file contains 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
<?php | |
/* | |
* A very basic example file that will use Advanced custom fields – http://www.advancedcustomfields.com/ | |
* along with Geotargeting Pro – https://timersys.com/geotargeting/ | |
* to display a simple image or iframe base on user Geolocation | |
*/ | |
?><div id="content"> | |
<?php /* wordpress loop */ ?> | |
<?php if(have_posts()) : while(have_posts()) : the_post(); ?> | |
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
<?php | |
// repeater field | |
if( have_rows('urls') ): | |
// loop through the rows of data | |
while ( have_rows('urls') ) : the_row(); | |
// geot field | |
// to debug you could print_r( $geot_games ); | |
$geot_games = get_sub_field('geotargeting'); | |
// text field | |
$game_url = get_sub_field('url'); | |
// radio field | |
$url_type = get_sub_field('url_type'); | |
// if geot_condition == include means "show to" | |
if( $geot_games['geot_condition'] == 'include' ) { | |
if( geot_target( '', $geot_games['geot_regions']) ){ | |
// a simple function I use to print the iframe/ image not needed on this example | |
show_game_frame( $game_url, $url_type ); | |
// if we have a match we simple get out of the repeater loop | |
break; | |
} | |
} else { | |
// we target the same countries but adding ! as we want to "exclude" matched regions | |
if( ! geot_target( '', $geot_games['geot_regions']) ) { | |
show_game_frame( $game_url, $url_type ); | |
break; | |
} | |
} | |
endwhile; | |
endif; | |
?> | |
</div> | |
<?php endwhile; endif;?> | |
</div> |
Leave your question on the comments and Happy New Year!!
Leave a Reply