• 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 = "https://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]
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Just testing here, but… what happens if you change EP_ROOT to EP_ALL?

    I’m asking becuse if it works with EP_ALL then you know that you’re OK and it was just the ‘places’ mask that wasn’t right and you can refine that to see what does and doesn’t work for what you need.

    Moderator bcworkz

    (@bcworkz)

    I’m not sure where that rewrite rule is coming from, but it’s not passing on the regex capture group as the ip query var.

    You don’t really need an added rewrite rule with an added endpoint, WP already creates one for the endpoint. But the default added rewrite rule does not tell WP which page to get that has your shortcode. You can use the “pre_get_posts” action to add the necessary p query var value anytime the ip query var exists.

    Or not use the endpoint and simply create your own custom rewrite rule which both specifies the right page and passes on the ip query var. If you go this route, you also need to whitelist ip through the “query_vars” filter.

    Either way, after updating your code, don’t forget to flush rewrite rules, or visit the permalinks settings screen.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating an IP lookup script’ is closed to new replies.