Below, you’ll find a detailed guide on how to add the Geolocation IP Detection Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Geolocation IP Detection Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Geolocation IP Detection Plugin and the shortcodes it provides:
"Geolocation IP Detection is a highly functional WordPress plugin designed to detect and display your user's IP-based location. It's perfect for personalizing content and enhancing user experience."
- [geoip_detect]
- [geoip_detect2_current_flag]
- [geoip_detect2_text_input]
- [geoip_detect2]
- [geoip_detect2_get_client_ip]
- [geoip_detect2_get_external_ip_adress]
- []
- [geoip_detect2_user_info]
- [geoip_detect2_enqueue_javascript]
- [geoip_detect2_countries_select]
- [geoip_detect2_show_if]
Geolocation IP Detection [geoip_detect] Shortcode
The GeoIP Detect shortcode retrieves geolocation data for the current IP. It uses the shortcode ‘geoip_detect’, defined by the function ‘geoip_detect_shortcode’. The function fetches user info based on the IP and returns the specified property value. If the property doesn’t exist or no info is found, it returns a default value.
Shortcode: [geoip_detect]
Parameters
Here is a list of all possible geoip_detect shortcode parameters and attributes:
default
– The fallback value if no geolocation data is found.property
– The specific geolocation property to retrieve.
Examples and Usage
Basic example – A simple usage of the geoip_detect shortcode to display the country of the user’s IP address.
[geoip_detect property="country_name" default="Unknown"]
Advanced examples
Using the shortcode to display the city of the user’s IP address. If the city cannot be determined, it will default to displaying “Location Unknown”.
[geoip_detect property="city" default="Location Unknown"]
Using the shortcode to display the latitude and longitude of the user’s IP address. If the geolocation data cannot be determined, it will default to displaying “Coordinates Unknown”.
[geoip_detect property="latitude" default="Coordinates Unknown"] [geoip_detect property="longitude" default="Coordinates Unknown"]
Using the shortcode to display the user’s IP address. If the IP address cannot be determined, it will default to displaying “IP Unknown”.
[geoip_detect property="ip" default="IP Unknown"]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect', 'geoip_detect_shortcode');
Shortcode PHP function:
function geoip_detect_shortcode($attr)
{
$userInfo = geoip_detect_get_info_from_current_ip();
$defaultValue = isset($attr['default']) ? $attr['default'] : '';
if (!is_object($userInfo))
return $defaultValue . '<!-- Geolocation IP Detection: No info found for this IP. -->';
$propertyName = $attr['property'];
if (property_exists($userInfo, $propertyName)) {
if ($userInfo->$propertyName)
return $userInfo->$propertyName;
else
return $defaultValue;
}
return $defaultValue . '<!-- Geolocation IP Detection: Invalid property name. -->';
}
Code file location:
geoip-detect/geoip-detect/deprecated.php
Geolocation IP Detection [geoip_detect2_current_flag] Shortcode
The GeoIP Detect shortcode is used to display the flag of the user’s current IP country. It works in conjunction with the “SVG Flags” plugin. If the plugin is missing, a placeholder comment will be displayed instead. The shortcode attributes include ‘width’, ‘height’, ‘squared’, ‘class’, ‘default’, ‘skip_cache’, and ‘ajax’. It allows customization of the flag’s appearance and behavior. If the ‘ajax’ attribute is set, the flag will be loaded asynchronously. If the country cannot be detected, the ‘default’ attribute will be used.
Shortcode: [geoip_detect2_current_flag]
Parameters
Here is a list of all possible geoip_detect2_current_flag shortcode parameters and attributes:
width
– sets the width of the flag imageheight
– determines the height of the flag imagesquared
– makes the flag image square-shapedsquare
– another option to make the flag image square-shapedclass
– adds custom CSS classes to the flag imagedefault
– sets a default flag to display when no country is detectedskip_cache
– bypasses the cache for real-time geoip detectionajax
– allows the flag image to be updated on the fly using AJAX
Examples and Usage
Basic example – Displaying the current user’s country flag without any additional parameters.
[geoip_detect2_current_flag /]
Advanced examples
Displaying the current user’s country flag with a specific width and height. The width and height are set to 50 pixels each.
[geoip_detect2_current_flag width="50" height="50" /]
Displaying the current user’s country flag in a squared format. The ‘squared’ attribute is set to ‘true’.
[geoip_detect2_current_flag squared="true" /]
Displaying the current user’s country flag with a custom CSS class. The ‘class’ attribute is set to ‘custom-flag’.
[geoip_detect2_current_flag class="custom-flag" /]
Displaying the current user’s country flag with a default flag to show when the user’s country cannot be detected. The ‘default’ attribute is set to ‘us’ for the United States.
[geoip_detect2_current_flag default="us" /]
Displaying the current user’s country flag without caching the result. The ‘skip_cache’ attribute is set to ‘true’.
[geoip_detect2_current_flag skip_cache="true" /]
Displaying the current user’s country flag and updating it with AJAX. The ‘ajax’ attribute is set to ‘true’.
[geoip_detect2_current_flag ajax="true" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_current_flag]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_current_flag', 'geoip_detect2_shortcode_current_flag');
Shortcode PHP function:
function geoip_detect2_shortcode_current_flag($orig_attr, $content = '', $shortcodeName = 'geoip_detect2_current_flag') {
if (!shortcode_exists('svg-flag') && !defined('GEOIP_DETECT_DOING_UNIT_TESTS')) {
return '<!-- There should be a flag here. However, the Plugin "SVG Flags" is missing.';
}
$attr = shortcode_atts(array(
'width' => '',
'height' => '',
'squared' => '',
'square' => '',
'class' => '',
'default' => '',
'skip_cache' => false,
'ajax' => false,
), $orig_attr, $shortcodeName);
$skipCache = filter_var($attr['skip_cache'], FILTER_VALIDATE_BOOLEAN );
$options = [ 'skipCache' => $skipCache ];
$style = '';
$processCssProperty = function($name, $value) {
$value = strtr($value, [' ' => '', ':' => '', ';' => '']);
if (!$value) {
return '';
}
if (is_numeric($value)) {
$value .= 'px';
}
return $name . ':' . $value . ';';
};
$style .= $processCssProperty('height', $attr['height']);
$style .= $processCssProperty('width', $attr['width']);
if ($attr['squared'] || $attr['square']) {
$attr['class'] .= ' flag-icon-squared';
}
$attr['class'] .= ' flag-icon';
$options = [];
if (geoip_detect2_shortcode_is_ajax_mode($orig_attr)) {
geoip_detect2_enqueue_javascript('shortcode');
$attr['class'] .= ' js-geoip-detect-flag';
$options['default'] = $attr['default'];
} else {
$record = geoip_detect2_get_info_from_current_ip(null, $options);
$country = $attr['default'];
if ($record->country->isoCode) {
$country = $record->country->isoCode;
}
if (!$country) {
return '<!-- There should be a flag here, but no country could be detected and the parameter "default" was not set. -->';
}
$country = mb_substr($country, 0, 2);
$country = mb_strtolower($country);
$attr['class'] .= ' flag-icon-' . $country;
}
return _geoip_detect2_create_placeholder('span', [
'style' => $style,
'class' => $attr['class'],
], $options);
}
Code file location:
geoip-detect/geoip-detect/shortcodes/flags.php
Geolocation IP Detection [geoip_detect2_text_input] Shortcode
The geoip_detect2_text_input shortcode, part of the GeoIP Detect plugin, generates a text input field. It allows customization of the input field attributes like name, id, class, type, and placeholder. The shortcode also supports AJAX mode, enabling dynamic data loading. It can autosave the input value and handle error messages. It returns the HTML code for the input field.
Shortcode: [geoip_detect2_text_input]
Parameters
Here is a list of all possible geoip_detect2_text_input shortcode parameters and attributes:
type
– defines the type of input element to be usedname
– sets the name attribute for the input fieldid
– assigns a unique identifier to the input elementclass
– assigns a class name to the input element for CSS stylingrequired
– sets if the input field is mandatory to fillinvalid
– sets the aria-invalid attribute, used for accessibilityplaceholder
– sets the placeholder text for the input fieldautosave
– sets whether to automatically save input field value
Examples and Usage
Basic example – An instance of the shortcode with minimal attributes, ideal for simple text input fields.
[geoip_detect2_text_input name="myInput" /]
Advanced examples
Creating a required text input field with a placeholder, a unique ID, and custom CSS class. The ‘required’ attribute ensures that the field must be filled before submission, and the ‘placeholder’ provides a hint to the user about what is expected in the field.
[geoip_detect2_text_input name="myInput" id="uniqueID" class="customClass" required="required" placeholder="Enter your text here" /]
Creating an email input field with autosave functionality enabled. This is useful when you want the user’s input to be saved automatically. The ‘type’ attribute is set to ’email’ to ensure that the input is validated as an email address.
[geoip_detect2_text_input name="myInput" type="email" autosave="true" /]
Creating a text input field with AJAX mode enabled. The ‘ajax’ attribute is set to ‘true’ to enable AJAX mode. This can be useful when you want to update the field’s value without refreshing the page.
[geoip_detect2_text_input name="myInput" ajax="true" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_text_input]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_text_input', 'geoip_detect2_shortcode_text_input');
Shortcode PHP function:
function geoip_detect2_shortcode_text_input($attr) {
$type = !empty($attr['type']) ? sanitize_key($attr['type']) : '';
$html_attrs = array(
'name' => !empty($attr['name']) ? $attr['name'] : 'geoip-text-input',
'id' => !empty($attr['id']) ? $attr['id'] : '',
'class' => !empty($attr['class']) ? $attr['class'] : 'geoip-text-input',
'type' => $type ? $type : 'text',
'aria-required' => !empty($attr['required']) ? 'required' : '',
'aria-invalid' => !empty($attr['invalid']) ? $attr['invalid'] : '',
'placeholder' => !empty($attr['placeholder']) ? $attr['placeholder'] : '',
);
if (geoip_detect2_shortcode_is_ajax_mode($attr)) {
geoip_detect2_enqueue_javascript('shortcode');
$html_attrs['class'] .= ' js-geoip-text-input';
if (!empty($attr['autosave'])) {
$html_attrs['class'] .= ' js-geoip-detect-input-autosave';
}
$html_attrs['data-options'] = wp_json_encode(_geoip_detect2_shortcode_options($attr));
} else {
$html_attrs['value'] = geoip_detect2_shortcode($attr + [ 'add_error' => false ]);
}
$html = '<input ' . _geoip_detect_flatten_html_attr($html_attrs) . '/>';
return $html;
}
Code file location:
geoip-detect/geoip-detect/shortcodes/input.php
Geolocation IP Detection [geoip_detect2] Shortcode
The GeoIP Detection shortcode provides geolocation data based on the IP address. It fetches user info, validates property names, and handles errors. The shortcode allows cache skipping, language specification, and IP address input. If no IP is provided, it defaults to the client’s IP. It also supports AJAX mode.
Shortcode: [geoip_detect2]
Parameters
Here is a list of all possible geoip_detect2 shortcode parameters and attributes:
skip_cache
– Determines if the plugin should bypass the cache.lang
– Sets the language for the geolocation data.default
– Provides a fallback value if geolocation data isn’t available.property
– Specifies the geolocation property to display.ip
– Allows manual input of an IP address for testing.add_error
– Decides if error messages should be displayed.
Examples and Usage
Basic example – Displaying the user’s geographical information without any specific parameters.
[geoip_detect2 /]
Advanced examples
Displaying the user’s geographical information for a specific IP address, skipping the cache, and specifying the language to be English.
[geoip_detect2 ip="123.45.67.89" skip_cache="true" lang="en" /]
Displaying a specific property of the user’s geographical information, in this case, the city, and providing a default value in case no information is found for the IP address.
[geoip_detect2 property="city" default="Unknown City" /]
Suppressing error messages when no information is found for the IP address or when an invalid property name is used.
[geoip_detect2 add_error="false" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2', 'geoip_detect2_shortcode');
Shortcode PHP function:
function geoip_detect2_shortcode($orig_attr, $content = '', $shortcodeName = 'geoip_detect2')
{
$attr = shortcode_atts(array(
'skip_cache' => 'false',
'lang' => null,
'default' => '',
'property' => '',
'ip' => null,
'add_error' => true,
), $orig_attr, $shortcodeName);
$shortcode_options = _geoip_detect2_shortcode_options($attr);
if (geoip_detect2_shortcode_is_ajax_mode($orig_attr) && !$attr['ip']) {
geoip_detect2_enqueue_javascript('shortcode');
return _geoip_detect2_create_placeholder('span', [
'class' => 'js-geoip-detect-shortcode'
], $shortcode_options);
}
$options = [ 'skipCache' => $shortcode_options['skip_cache'] ];
$ip = $attr['ip'] ?: geoip_detect2_get_client_ip();
$userInfo = geoip_detect2_get_info_from_ip($ip, $shortcode_options['lang'], $options);
$defaultValue = esc_html($attr['default']);
if ($userInfo->isEmpty)
return $defaultValue . ($attr['add_error'] ? '<!-- Geolocation IP Detection: No information found for this IP (' . geoip_detect2_get_client_ip() . ') -->' : '');
try {
$return = geoip_detect2_shortcode_get_property($userInfo, $attr['property']);
} catch (\RuntimeException $e) {
return $defaultValue . ($attr['add_error'] ? '<!-- Geolocation IP Detection: Invalid property name. -->' : '');
}
if (is_object($return) && $return instanceof \GeoIp2\Record\AbstractPlaceRecord) {
$return = $return->name;
}
if (is_object($return) || is_array($return)) {
return $defaultValue . ($attr['add_error'] ? '<!-- Geolocation IP Detection: Invalid property name (sub-property missing). -->' : '');
}
if ($return)
return (string) $return;
else
return $defaultValue;
}
Code file location:
geoip-detect/geoip-detect/shortcodes/main.php
Geolocation IP Detection [geoip_detect2_get_client_ip] Shortcode
The GeoIP Detection plugin shortcode ‘geoip_detect2_get_client_ip’ is designed to fetch the client’s IP address. It can operate in AJAX mode, returning IP data asynchronously. If AJAX mode isn’t active, the shortcode retrieves the client’s IP directly, normalizing it for consistent formatting. This ensures accurate geolocation data. Shortcode: [geoip_detect2_get_client_ip]
Shortcode: [geoip_detect2_get_client_ip]
Parameters
Here is a list of all possible geoip_detect2_get_client_ip shortcode parameters and attributes:
property
– It determines the specific property of the user’s IP address to be returned.ajax
– It enables or disables the AJAX mode of the shortcode.
Examples and Usage
Basic example – Showcases the application of the geoip_detect2_get_client_ip shortcode without any additional parameters.
[geoip_detect2_get_client_ip /]
Advanced examples
Illustrates the use of the geoip_detect2_get_client_ip shortcode with an AJAX attribute. This will enable the IP address to be fetched asynchronously without having to reload the page.
[geoip_detect2_get_client_ip ajax=true /]
Demonstrates the usage of the geoip_detect2_get_client_ip shortcode with AJAX attribute set to false. This will result in the IP address being fetched synchronously, requiring a page reload.
[geoip_detect2_get_client_ip ajax=false /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_get_client_ip]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_get_client_ip', 'geoip_detect2_shortcode_client_ip');
Shortcode PHP function:
function geoip_detect2_shortcode_client_ip($attr) {
if (geoip_detect2_shortcode_is_ajax_mode($attr)) {
return geoip_detect2_shortcode([
'property' => 'traits.ipAddress',
'ajax' => isset($attr['ajax']) ? $attr['ajax'] : null,
]);
} else {
$client_ip = geoip_detect2_get_client_ip();
$client_ip = geoip_detect_normalize_ip($client_ip);
return $client_ip;
}
}
Code file location:
geoip-detect/geoip-detect/shortcodes/other.php
Geolocation IP Detection [geoip_detect2_get_external_ip_adress] Shortcode
The ‘geoip_detect2_get_external_ip_adress’ shortcode retrieves the external IP address of a user. It employs the geoip_detect2_get_external_ip_adress() function to fetch the IP, then normalizes it using the geoip_detect_normalize_ip() function.
Shortcode: [geoip_detect2_get_external_ip_adress]
Examples and Usage
Basic example – An instance of using the shortcode to retrieve and display the external IP address.
[geoip_detect2_get_external_ip_adress /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_get_external_ip_adress]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_get_external_ip_adress', 'geoip_detect2_shortcode_get_external_ip_adress');
Shortcode PHP function:
function geoip_detect2_shortcode_get_external_ip_adress($attr) {
$external_ip = geoip_detect2_get_external_ip_adress();
$external_ip = geoip_detect_normalize_ip($external_ip);
return $external_ip;
}
Code file location:
geoip-detect/geoip-detect/shortcodes/other.php
Geolocation IP Detection [geoip_detect2_user_info] Shortcode
The GeoIP Detection plugin shortcode is a tool that retrieves user’s geographical information. Shortcode Name: geoip_detect2_user_info This shortcode fetches and displays user’s IP-based geolocation data. It can be used to customize content based on the user’s location.
Shortcode: [geoip_detect2_user_info]
Examples and Usage
Basic example – Showcases the usage of the shortcode ‘geoip_detect2_user_info’ without any additional parameters.
[geoip_detect2_user_info /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_user_info]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_user_info', 'geoip_detect_shortcode_user_info');
Shortcode PHP function:
function geoip_detect_shortcode_user_info() {
return geoip_detect2_shortcode_user_info_wpcf7('', 'geoip_detect2_user_info', true);
}
Code file location:
geoip-detect/geoip-detect/shortcodes/other.php
Geolocation IP Detection [geoip_detect2_enqueue_javascript] Shortcode
The GeoIP Detect shortcode is a tool that enqueues the JavaScript file into your WordPress site. It is primarily used for location-based services. This shortcode is activated by adding it to your post or page. Once activated, it calls the ‘geoip_detect2_enqueue_javascript’ function, which loads the necessary JavaScript file. This allows your site to access geolocation data and provide personalized content based on the user’s location.
Shortcode: [geoip_detect2_enqueue_javascript]
Examples and Usage
Basic example – Enqueues the Javascript required for the ‘geoip-detect2’ plugin, using the ‘user_shortcode’ parameter.
[geoip_detect2_enqueue_javascript user_shortcode /]
Geolocation IP Detection [geoip_detect2_countries_select] Shortcode
The GeoIP Detect plugin shortcode generates a dropdown list of countries. It uses the user’s current IP address to pre-select their country. The list can be customized with flags, telephone prefixes, or a specified order. It also supports AJAX mode for dynamic updates. The shortcode is ideal for form fields that require country selection.
Shortcode: [geoip_detect2_countries_select]
Parameters
Here is a list of all possible geoip_detect2_countries_select shortcode parameters and attributes:
property
– Specifies the geoip property to use, in this case ‘country.name’name
– Assigns a unique name to the country select dropdownid
– Unique identifier of the select dropdownclass
– Assigns a CSS class to the select dropdownrequired
– Determines if the select dropdown is requiredinvalid
– Specifies the invalid status of the select dropdownselected
– Pre-selects a country in the dropdowndefault
– Sets a default country if none is selectedlist
– Defines a custom list of countriesflag
– If set, adds country flags to the dropdowntel
– If set, adds country telephone prefixes to the dropdowninclude_blank
– If set, includes a blank option in the dropdown
Examples and Usage
Basic example – Show a dropdown list of countries with their names.
[geoip_detect2_countries_select /]
Advanced examples
Display a dropdown list of countries with their names and flags. The dropdown list has a custom CSS class, ‘my-custom-class’, and a specified default country, ‘US’.
[geoip_detect2_countries_select flag=true class="my-custom-class" default="US" /]
Display a dropdown list of countries with their names and telephone prefixes. The dropdown list has a custom CSS class, ‘my-custom-class’, and a specified default country, ‘US’. It also includes a blank option at the beginning of the list.
[geoip_detect2_countries_select tel=true class="my-custom-class" default="US" include_blank=true /]
Display a dropdown list of countries with their names, flags, and telephone prefixes. The dropdown list has a custom CSS class, ‘my-custom-class’, and a specified default country, ‘US’. It also includes a blank option at the beginning of the list and only shows the countries specified in the ‘list’ attribute.
[geoip_detect2_countries_select flag=true tel=true class="my-custom-class" default="US" include_blank=true list="US,CA,MX" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_countries_select]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_countries_select', 'geoip_detect2_shortcode_country_select');
Shortcode PHP function:
function geoip_detect2_shortcode_country_select($attr) {
$attr['property'] = 'country.name';
$shortcode_options = _geoip_detect2_shortcode_options($attr);
$select_attrs = [
'name' => !empty($attr['name']) ? $attr['name'] : 'geoip-countries',
'id' => !empty($attr['id']) ? $attr['id'] : '',
'class' => !empty($attr['class']) ? $attr['class'] : 'geoip_detect2_countries',
'aria-required' => !empty($attr['required']) ? 'required' : '',
'aria-invalid' => !empty($attr['invalid']) ? $attr['invalid'] : '',
'autocomplete' => 'off',
];
$selected = '';
if (geoip_detect2_shortcode_is_ajax_mode($attr) && !isset($attr['selected']) ) {
geoip_detect2_enqueue_javascript('shortcode');
$select_attrs['class'] .= ' js-geoip-detect-country-select';
if (!empty($attr['autosave'])) {
$select_attrs['class'] .= ' js-geoip-detect-input-autosave';
}
$select_attrs['data-options'] = wp_json_encode($shortcode_options);
} else {
if (!empty($attr['selected'])) {
$selected = $attr['selected'];
} else {
$record = geoip_detect2_get_info_from_current_ip();
$selected = $record->country->isoCode;
}
if (empty($selected)) {
if (isset($attr['default']))
$selected = $attr['default'];
}
}
$countryInfo = new YellowTree\GeoipDetect\Geonames\CountryInformation();
$countries = $countryInfo->getAllCountries($shortcode_options['lang']);
if (!empty($attr['list'])) {
$list = wp_parse_list($attr['list']);
$allCountries = $countries;
$countries = [];
foreach($list as $key) {
if (str_starts_with($key,'blank_')) {
$countries[$key] = str_replace('_', ' ', mb_substr($key, 6));
} else {
$key = mb_strtoupper($key);
if (isset($allCountries[$key])) {
$countries[$key] = $allCountries[$key];
}
}
}
if ($selected && !isset($countries[$selected])) {
if (isset($attr['default'])) {
$selected = '';
}
}
}
if (!empty($attr['flag'])) {
array_walk($countries, function(&$value, $key) use($countryInfo) {
$flag = $countryInfo->getFlagEmoji($key);
$value = $flag . ' ' . $value;
});
}
if (!empty($attr['tel'])) {
array_walk($countries, function(&$value, $key) use($countryInfo) {
$tel = $countryInfo->getTelephonePrefix($key);
if ($tel) {
$value = $value . ' (' . $tel . ')';
}
});
}
/**
* Filter: geoip_detect2_shortcode_country_select_countries
* Change the list of countries that should show up in the select box.
* You can add, remove, reorder countries at will.
* If you want to add a blank value (for seperators or so), use a key name that starts with 'blank_'
* and then something at will in case you need several of them.
*
* @param array $countries List of localized country names
* @param array $attr Parameters that were passed to the shortcode
* @return array
*/
$countries = apply_filters('geoip_detect2_shortcode_country_select_countries', $countries, $attr);
$html = '<select ' . _geoip_detect_flatten_html_attr($select_attrs) . '>';
if (!empty($attr['include_blank']) && $attr['include_blank'] !== 'false') {
$html .= '<option value="">---</option>';
}
foreach ($countries as $code => $label) {
$code = mb_strtolower($code);
if (str_starts_with($code,'blank_'))
{
$html .= '<option data-c="" value="">' . esc_html($label) . '</option>';
}
else
{
$html .= '<option data-c="' . esc_attr($code). '"' . ($code == mb_strtolower($selected) ? ' selected="selected"' : '') . '>' . esc_html($label) . '</option>';
}
}
$html .= '</select>';
return $html;
}
Code file location:
geoip-detect/geoip-detect/shortcodes/select.php
Geolocation IP Detection [geoip_detect2_show_if] Shortcode
The GeoIP Detect plugin shortcode, ‘geoip_detect2_show_if’, is a conditional shortcode that displays content based on the user’s geographical location. This shortcode evaluates the user’s IP address to determine their location. It then displays specific content if the conditions set in the attributes match the user’s location. If the conditions do not match, it displays alternative content. This shortcode also supports AJAX mode for dynamic content loading.
Shortcode: [geoip_detect2_show_if]
Parameters
Here is a list of all possible geoip_detect2_show_if shortcode parameters and attributes:
skipCache
– If set, bypasses the cache and fetches the fresh location data.else_seperator
– Determines the string that separates the ‘if’ and ‘else’ content parts.lang
– Determines the language used for location data.not
– If set to 1, negates the conditions specified in the shortcode.
Examples and Usage
Basic example – Display content based on the user’s geographical location.
[geoip_detect2_show_if country="US"]Content for US visitors[/geoip_detect2_show_if]
This shortcode will display the enclosed content only if the visitor’s detected country is the United States (US). The ‘country’ attribute is used to specify the country code.
Advanced examples
Display different content based on the user’s geographical location with an ‘else’ separator.
[geoip_detect2_show_if country="US"]Content for US visitors[else]Content for non-US visitors[/geoip_detect2_show_if]
In this example, the shortcode will display the first part of the content if the visitor’s detected country is the United States. If the visitor is from any other country, the content after the ‘else’ separator will be displayed.
Using multiple conditions to display content based on the user’s city and country.
[geoip_detect2_show_if country="US" city="New York"]Content for New York, US visitors[/geoip_detect2_show_if]
This advanced usage of the shortcode allows you to display content based on both the visitor’s city and country. In this case, the enclosed content will only be displayed if the visitor’s detected location is New York, United States.
PHP Function Code
In case you have difficulties debugging what causing issues with [geoip_detect2_show_if]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('geoip_detect2_show_if', 'geoip_detect2_shortcode_show_if');
Shortcode PHP function:
function geoip_detect2_shortcode_show_if($attr, $content = '', $shortcodeName = '') {
$shortcode_options = _geoip_detect2_shortcode_options($attr);
$options = [ 'skipCache' => $shortcode_options['skip_cache'] ];
$showContentIfMatch = ($shortcodeName === 'geoip_detect2_show_if');
$attr = (array) $attr;
$else_seperator = apply_filters('geoip_detect2_shortcode_show_if_else_seperator', '[else]');
$parts = explode($else_seperator, $content, 2);
$content_if = $parts[0];
$content_else = isset($parts[1]) ? $parts[1] : '';
$parsed = geoip_detect2_shortcode_parse_conditions_from_attributes($attr, !$showContentIfMatch);
if (geoip_detect2_shortcode_is_ajax_mode($attr)) {
geoip_detect2_enqueue_javascript('shortcode');
$shortcode_options['parsed'] = $parsed;
$span_attributes = [ 'class' => 'js-geoip-detect-show-if', 'style' => 'display: none !important' ];
$span_if = _geoip_detect2_create_placeholder('span', $span_attributes, $shortcode_options, do_shortcode($content_if));
$shortcode_options['parsed']['not'] = ($shortcode_options['parsed']['not'] === 1 ? 0 : 1); // negate
$span_else = _geoip_detect2_create_placeholder('span', $span_attributes, $shortcode_options, do_shortcode($content_else));
return $span_if . $span_else;
} else {
$info = geoip_detect2_get_info_from_current_ip($shortcode_options['lang'], $options);
/**
* You can override the detected location information here.
* E.g. "Show if in Paris, but if the user has given an adress in his profile, use that city instead"
* (Does not work in AJAX mode)
*
* @param YellowTree\GeoipDetect\DataSources\City $info
* @param array $attr Shortcode attributes given to the function.
* @param bool $showContentIfMatch Should the content be shown (TRUE) or hidden (FALSE) if the conditions are true?
*/
$info = apply_filters('geoip_detect2_shortcode_show_if_ip_info_override', $info, $attr, $showContentIfMatch);
$evaluated = geoip_detect2_shortcode_evaluate_conditions($parsed, $info);
if ($evaluated) {
return do_shortcode($content_if);
} else {
return do_shortcode($content_else);
}
}
}
Code file location:
geoip-detect/geoip-detect/shortcodes/show_if.php
Conclusion
Now that you’ve learned how to embed the Geolocation IP Detection Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply