0

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] => -
)

1 Answer 1

1

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 -->
7
  • Thank You so much this worked. You are very thorough. only one thing which was probably a typo . esc_html(region_name) is missing $ infront of region_name. Aug 25, 2018 at 18:53
  • Hi @nmr its a long shot but i wanted to see if you know the answer to this. I added the ip2location how you had instructed and it works flawlessly but when I open the site from an ipv6 address (Using cell phone data usually that's Ipv6) the site is blank. Do you know of an apparent reason you can think of why? newprelaunch.centsai.com/buyers-guides/credit-repair Aug 29, 2018 at 15:37
  • Try enable debug and paste error message. Add or uncomment in wp-config.php define( 'WP_DEBUG', true ), define( 'WP_DEBUG_LOG', true ). This will log errors to /wp-content/debug.log and display on page. If you also add define( 'WP_DEBUG_DISPLAY', false ) errors will be only in log file. See here. @DebanjanGhosh
    – nmr
    Aug 29, 2018 at 15:55
  • Thank you for your advise, 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. Aug 31, 2018 at 17:43
  • I added the debug file as an edit on my original post. @nmr Aug 31, 2018 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.