I’ll keep this short and sweet, as mostly this is just to highlight a plugin that we found useful for serving content based on location for WordPress — geolocation or country filtering or whatever you would like to call it.
Our problem: We needed to serve different prices based on whether a user was in a European country, Great Britain, or default to the United States for all others. The other caveat was that we needed to do this within an actual page in WordPress via the WYSIWYG Editor.
First we attempted with a plugin WP Geocode Filter. Although this works great with ability to use shortcodes to provide content based on country, it did not have the ability to make “else if” and “else” statements so that if no country matched or if the country could not be detected that it would default to the US Dollar.
So realizing this shortcoming, we had two options. 1. write our own plugin to solve the issue, or 2. possibly use another plugin called Country Filter WordPress Plugin and use in page PHP to execute the “if else” and “else” statements. We decided to first test out Country Filter WordPress Plugin as it would be relatively quick to test and if it worked save the client time and money from building from scratch. In order to execute the PHP we also installed Allow PHP in Posts & Pages plugin (visit the plugin to see more about how to include PHP in your pages/ posts).
On our quick and dirty test of the plugin, everything worked smoothly. Essentially our code within WordPress’ WYSIWYG Editor looked something like the below.
if (function_exists('isCountryInFilter')) { if(isCountryInFilter(array("al","ad","am","at","by","be","ba","bg","ch","cy","cz","de","dk","ee","es","fo","fi","fr","ge","gi","gr","hu","hr","ie","is","it","lt","mc","mk","mt","no","nl","pl","pt","ro","ru","se","si","sk","sm","tr","ua","va"))) { echo '[h1 style="text-align:center"]€0[/h1]'; } elseif(isCountryInFilter(array("gb"))) { echo '[h1 style="text-align:center"]£0[/h1]'; } else { echo '[h1 style="text-align:center"]$0[/h1]'; } }
I won’t walk you through all the options here. This post is more to hopefully help those in search of similar functionality and steer them in the right direction, as well as validate that the plugin works and is worth the $18 price tag (and BTW I nor Mackey have any affiliation with the plugin author).
To view more about the Country Filter Plugin, go to https://www.codediesel.com/plugins/country-filter/