0

I'd like to implement the ability to incorporate an API into my Wordpress blog that allows people to perform an IP lookup within my blog. So far all of my attempts have failed when incorporating the function to Wordpress. As a standalone code, it works just fine. The goal is to use a domain.com/ip/1.1.1.1 and my attempts either show a 404, redirect to home page, or just show most recent posts.

Here's what I have:

custom rewrite endpoint

function ip_info_rewrite_endpoint() {
    add_rewrite_endpoint('ip', EP_ROOT);
}

add_action('init', 'ip_info_rewrite_endpoint');
functions.php

function ip_info_shortcode() {
    global $wp_query;
    $ip = $wp_query->get('ip');

    if (!$ip) {
        return "<h1>Please provide an IP address</h1>";
    }

    $url = "http://ip-api.com/php/{$ip}?fields=status,message,continent,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,isp,org,as,asname,reverse,proxy,hosting,query";
    $response = file_get_contents($url);
    $data = unserialize($response);

    if ($data['status'] == 'fail') {
        return "<h1>IP Information Not Found</h1>";
    } else {
        $output = "<h1>IP Information for {$data['query']}</h1>";
        $output .= "<table>";
        $output .= "<tr><th>IP Address</th><td>{$data['query']}</td></tr>";
        $output .= "<tr><th>Continent</th><td>{$data['continent']}</td></tr>";
        $output .= "<tr><th>Country</th><td>{$data['country']}</td></tr>";
        $output .= "<tr><th>Region</th><td>{$data['regionName']}</td></tr>";
        $output .= "<tr><th>City</th><td>{$data['city']}</td></tr>";
        $output .= "<tr><th>ZIP Code</th><td>{$data['zip']}</td></tr>";
        $output .= "<tr><th>Latitude</th><td>{$data['lat']}</td></tr>";
        $output .= "<tr><th>Longitude</th><td>{$data['lon']}</td></tr>";
        $output .= "<tr><th>Time Zone</th><td>{$data['timezone']}</td></tr>";
        $output .= "<tr><th>ISP</th><td>{$data['isp']}</td></tr>";
        $output .= "</table>";
        return $output;
    }
}
add_shortcode('ip_info', 'ip_info_shortcode');

RewriteRule ^ip/([0-9\.]+)/?$ /index.php?page_id=2529 [L,QSA]

1 Answer 1

0

You'd have to use add_rewrite_rule so you can capture your desired url match and pass it on query_vars

Something like;

add_action('init', function() {
    add_rewrite_rule('ip/(.+)/?$','index.php?ip_search=$matches[1]','top');
});

add_filter( 'query_vars',  function( $vars ) {
    $vars[] = 'ip_search';
    return $vars;
});

dont forget to flush the rewrite rule once you added your own rule, change the permalink settings back and fort or just call the flush_rewrite_rules after the rule been added

Then the next step is how to handle if ip_search param is present in query var,

One option is to create a custom template inside your plugin,

e.i, your-plugin-name/some-folder/ip-lookup.php

Then you could use template_include and add your API related code inside that template.

Another option is to create a page without content, then query that page if your ip_search is present in query var and hijack the content using the_content filter and replace it with the output of your function

e.i.

// Query that page when ip_search query is present
add_action( 'wp', function() {

    global $wp_query, $post;

    if ( !isset( $wp_query->query['ip_search'] ) )
        return;
        
    $args=[
        'post_type' => 'page', 
        'p' =>  116, // The page ID you want to query if your custom rewrite rule is match
        'ip' => $wp_query->query['ip_search'], //pass in the ip just so you can capture on the_content filter
    ];
        
    $wp_query = new WP_Query( $args ); 
        
    $wp_query->is_single = false; //tell wordpress its a single page
    $wp_query->is_page = 1; // tell wordpress its a page
    $post = $wp_query->posts[0]; // assign first post to global $post
});

//Hijack the content if the query contains an IP
add_filter( 'the_content', function($content) {
    global $wp_query;

    if (  !isset( $wp_query->query['ip'] ) )
        return $content;
    // Call your function and pass the in the IP
    return ip_info_shortcode( $wp_query->query['ip'] );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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