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:
Leave your question on the comments and Happy New Year!!