I’m assuming you’re using “IP2Location Tag WordPress Plugin”.
In main file of IP2Location plugin, the class instance is created and assigned to the variable.
$ip2location_tags = new IP2LocationTags();
To use the plugin in theme, just use the global variable $ip2location_tags
.
Remember to make sure that the class IP2LocationTags
exists first and use global
keyword.
if (class_exists('IP2LocationTags')) {
global $ip2location_tags;
$location = $ip2location_tags->get_location('IP_ADDRESS');
}
The plugin sets the IP address in this way (in parse_content
method):
$ip_address = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
Keys in the location data table:
ipAddress,
countryCode,
countryName,
regionName,
cityName,
latitude,
longitude,
isp,
domainName,
zipCode,
timeZone,
netSpeed,
iddCode,
areaCode,
weatherStationCode,
weatherStationName,
mcc,
mnc,
mobileCarrierName,
elevation,
usageType,
In functions.php
file add function:
if ( !function_exists( 'ip2loc_get_region' ) ) {
function ip2loc_get_region() {
global $ip2location_tags;
if ( !class_exists('IP2LocationTags') )
return '';
$ip_address = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$location = $ip2location_tags->get_location($ip_address);
if ( !$location )
return '';
return $location['regionName'];
}
}
File header.php
:
<?php
$def_header_text = esc_html__( 'This is Header Media Text.', 'cleanportfolio' );
if ( current_user_can( 'edit_theme_options' ) ) {
$def_header_text .= ' ' . esc_html__( 'Edit this from Appearance - Customize - Header Media - Header Media Text.', 'cleanportfolio' );
}
$header_media_text = get_theme_mod( 'cleanportfolio_header_media_text', $def_header_text );
if ( is_front_page() && ( has_custom_header() || '' !== $header_media_text ) ) : ?>
<?php // text to display
$region_name = " " . ip2loc_get_region(); ?>
<div class="custom-header">
<?php
if ( has_custom_header() ) : ?>
<div class="custom-header-media">
<?php the_custom_header_markup(); ?>
</div>
<?php endif; ?>
<div class="custom-header-content sections header-media-section">
<?php if ( '' !== $header_media_text ) : ?>
<h2 class="section-title"><?php echo esc_html( $header_media_text ) . esc_html($region_name); ?></h2>
<?php endif; ?>
</div>
<?php if ( ! has_custom_header() ) : ?>
<div class="square"><?php echo cleanportfolio_get_svg( array(
'icon' => 'square',
) ); ?><span class="screen-reader-text"><?php esc_html_e( 'Square', 'cleanportfolio' ); ?></span></div>
<?php endif; ?>
</div><!-- .custom-header -->