首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用API开发IP查找函数

使用API开发IP查找函数
EN

WordPress Development用户
提问于 2023-04-18 00:38:31
回答 1查看 23关注 0票数 0

我想要实现将API合并到Wordpress博客中的能力,它允许人们在我的博客中执行IP查找。到目前为止,在将函数合并到Wordpress时,我的所有尝试都失败了。作为一个独立的代码,它工作得很好。目标是使用domain.com/ip/1.1.1.1,我的尝试要么显示404,要么重定向到主页,要么只显示最近的帖子。

我现在拥有的是:

代码语言:javascript
复制
custom rewrite endpoint

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

add_action('init', 'ip_info_rewrite_endpoint');
代码语言:javascript
复制
functions.php

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

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

    $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 "IP Information Not Found";
    } else {
        $output = "IP Information for {$data['query']}";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "";
        $output .= "IP Address{$data['query']}Continent{$data['continent']}Country{$data['country']}Region{$data['regionName']}City{$data['city']}ZIP Code{$data['zip']}Latitude{$data['lat']}Longitude{$data['lon']}Time Zone{$data['timezone']}ISP{$data['isp']}";
        return $output;
    }
}
add_shortcode('ip_info', 'ip_info_shortcode');
代码语言:javascript
复制
RewriteRule ^ip/([0-9\.]+)/?$ /index.php?page_id=2529 [L,QSA]
EN

回答 1

WordPress Development用户

发布于 2023-04-18 17:21:02

您必须使用添加_重写_规则,这样才能捕获所需的url匹配并在查询_瓦尔斯上传递它。

类似的东西;

代码语言:javascript
复制
add_action('init', function() {
    add_rewrite_rule('ip/(.+)/?添加规则后,不要忘记刷新重写规则,更改permalink设置,或者添加规则后只调用flush_rewrite_rules。接下来的步骤是如何处理查询var中存在的ip_search param,一种选择是在插件中创建一个自定义模板,e.i,your-plugin-name/some-folder/ip-lookup.php然后您可以使用模板_包括并在该模板中添加与API相关的代码。另一个选项是创建一个没有内容的页面,然后如果查询var中存在ip_search,然后使用这个_内容筛选器劫持内容,然后用函数的输出替换该页面。埃伊岛// 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'] );
});,'index.php?ip_search=$matches[1]','top');
});

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

添加规则后,不要忘记刷新重写规则,更改permalink设置,或者添加规则后只调用D3

接下来的步骤是如何处理查询var中存在的ip_search param,

一种选择是在插件中创建一个自定义模板,

e.i,D4

然后您可以使用C5并在该模板中添加与API相关的代码。

另一个选项是创建一个没有内容的页面,然后如果查询var中存在ip_search,然后使用C6筛选器劫持内容,然后用函数的输出替换该页面。

埃伊岛

A7

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/415430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档