Ip2location plugin in my template header?

I am using Ip2location plugin to pull state data based on Ip address into my post.

This is easy you install the plugin and then just use {ip:regionName} in the post or page editor.

But I want to print this information in my template header file right beside the title

is this possible and if so how do I go about this?

This is how the header php looks like in the template

        ?php

$def_header_text = esc_html__( 'This is Header Media Text.', 'cleanportfolio' );

if ( current_user_can( 'edit_theme_options' ) ) {
    $def_header_text .= 'nbsp;' . 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 ) ) : ?
    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 ); ?/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 --

this is how ip2location says to use there service in PHP

https://www.ip2location.com/developers/php

I am not a full time coder any example on how to may be try will be very helpful.

Error on cell Phone data

Thank you for your advise @nmr, I found quite a bit of bugs on other plugins so I decided to fix them to see if it would make ip2location work but it didn't.

Wifi / ethernet Connection: (This works great)

2018-08-31 17:39:53 Lookup by BIN database.
2018-08-31 17:39:53 Geolocation result for [66.219.198.137] found: Array
(
    [ipNumber] = 1121699465
    [ipVersion] = 4
    [ipAddress] = 66.219.198.137
    [countryName] = United States
    [countryCode] = US
    [cityName] = Lehi
    [regionName] = Utah
)

Cell Phone LTE Data Connection: (This Dosen't work )

2018-08-31 17:40:04 Lookup by BIN database.
2018-08-31 17:40:31 Lookup by BIN database.
2018-08-31 17:40:31 Geolocation result for [127.0.0.1] found: Array
(
    [ipNumber] = 2130706433
    [ipVersion] = 4
    [ipAddress] = 127.0.0.1
    [countryName] = -
    [countryCode] = -
    [cityName] = -
    [regionName] = -
)

Topic geo-data templates Wordpress

Category Web


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 .= '&nbsp;' . 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 -->

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.