<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>文章作者相关文章列表 | 皇家元林</title>
	<atom:link href="https://hjyl.org/tags/%E6%96%87%E7%AB%A0%E4%BD%9C%E8%80%85/feed/" rel="self" type="application/rss+xml" />
	<link>https://hjyl.org</link>
	<description>刘元林的个人博客</description>
	<lastBuildDate>Tue, 26 Mar 2024 01:00:34 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://img.hjyl.org/uploads/2019/10/cropped-about-me-32x32.png</url>
	<title>文章作者相关文章列表 | 皇家元林</title>
	<link>https://hjyl.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress发布文章显示IP归属地</title>
		<link>https://hjyl.org/wordpress-post-get-ip-location/</link>
					<comments>https://hjyl.org/wordpress-post-get-ip-location/#comments</comments>
		
		<dc:creator><![CDATA[皇家元林]]></dc:creator>
		<pubDate>Mon, 25 Mar 2024 16:37:10 +0000</pubDate>
				<category><![CDATA[元林手札]]></category>
		<category><![CDATA[IP归属地]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[文章作者]]></category>
		<category><![CDATA[纯真IP库]]></category>
		<guid isPermaLink="false">https://hjyl.org/?p=4994</guid>

					<description><![CDATA[之前呢，利用纯真IP库实现评论者IP归属地。最近突然想折腾一下，或许是在扬州行的想法吧，像朋友圈那样，可以显示 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>之前呢，利用<a href="https://hjyl.org/qqwry-ip-location/">纯真IP库实现评论者IP归属地</a>。最近突然想折腾一下，或许是在扬州行的想法吧，像朋友圈那样，可以显示发布信息的定位，以后或许能将这些定位串起来，形成一个轨迹，也许这也是人生的轨迹。本想直接找段代码直接来用的，没找到，或许大家没有这样的需求。后来还是找到了一款插件，叫apoyl-ip，有兴趣可以去看看。看了下代码，他是将IP地址记录到数据库里，然后读取数据库信息来显示IP归属地的。我感觉有点复杂了（看了下数据库才发现，评论的IP信息是有数据库字段的，文章作者却没有，希望官方可以考虑加一个）。</p>



<p>于是我想到了另外一种方法——自定义字段。没错，就是在发布文章时将作者的IP地址记录到自定义字段里，然后get字段，转换为归属地。好，思路有了，说干就干。</p>



<p>演示见本文标题下所显示的城市。</p>



<h3 class="wp-block-heading">获取作者发布文章时的IP</h3>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">//在发布文章时添加作者IP字段，开始
add_action('publish_post', 'get_the_author_ip');
function get_the_author_ip($post_id) {
    // 添加自定义字段
    $meta_key = 'hjylIP'; // 自定义字段名称
    $meta_value = $_SERVER['REMOTE_ADDR']; // 自定义字段值

    // 添加自定义字段到文章
    add_post_meta($post_id, $meta_key, $meta_value, true);
}
//在发布文章时添加作者IP字段，结束</code></pre>



<p>这个简单吧，WP官网上抄来的，其中关键的是$_SERVER['REMOTE_ADDR']，正常都是这个，如果是用代理或者反向代理，可能需要根据实际情况修改，譬如$_SERVER['HTTP_CLIENT_IP']或者$_SERVER['HTTP_X_FORWARDED_FOR']。</p>



<h3 class="wp-block-heading">将IP信息转换为归属地信息</h3>



<p>这个就用现成的，利用之前的纯真IP库同样可以实现。纯真IP库有个好处就是每周都会更新数据库，而且社区版是免费的，只是准确率没有收费版的精确。这也是我为什么在自定义字段里记录IP，而不直接记录归属地信息。具体可以了解之前的文章（<a href="https://hjyl.org/qqwry-ip-location/">点此跳转</a>），这里修改部分代码。</p>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">//文章作者发文归属地函数
function getAuthorIp($ip){
    if(empty($ip)) $ip = get_post_meta($post-&gt;ID,"hjylIP", true);
    $qqwry_filepath = get_template_directory() .'/qqwry.dat';
    $getLocation = json_encode(IpLocation::getLocation($ip, $qqwry_filepath), JSON_UNESCAPED_UNICODE) . "\n";
    $IPinfo = json_decode($getLocation, true);
    $country = $IPinfo['country'];
    $province = $IPinfo['province'];
    //优先显示城市，其次省，最后国家
    $city = $IPinfo['city'];
    if($ip == '127.0.0.1'){
        $data = '小黑屋';
    }elseif($city != ""){
        $data = $city;
    }elseif($province !== "" &amp;&amp; $city == ""){
        $data = $province;
    }else{
        $data = $country;
    }
    
    return $data;
}</code></pre>



<p>这里与评论不同的是精确到了城市，所以希望纯真IP越做越精确啊，这样我的人生轨迹也完整和精确。</p>



<h3 class="wp-block-heading">调用IP归属地代码</h3>



<p>在single.php你想放的位置调用归属地代码即可。</p>



<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">&lt;?php 
	if(function_exists('getAuthorIp') &amp;&amp; !empty(get_post_meta($post->ID,"hjylIP", true))) {
		echo 'φ';
		echo getAuthorIp(get_post_meta($post->ID,"hjylIP", true));
	}//支持文章作者IP归属地
?></code></pre>



<p>别看这么简单的代码，搞得我一身汗，可能大家需求没那么高，毕竟IP这个东西也算是隐私。当然希望对有需求的人有所帮助。可能有人会问，为什么以前的文章没有归属地信息呢？因为之前没有IP信息记录啊！</p>
<div id="content-copyright"><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;font-size: 13px;">版权声明: </span><span style="font-size: 13px;">本文采用 <a href="https://hjyl.org/go/aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzMuMC8=" rel="nofollow" target="_blank">BY-NC-SA</a> 协议进行授权，如无注明均为原创，转载请注明转自 <a href="https://hjyl.org">皇家元林</a><br>本文链接: <a rel="bookmark" title="WordPress发布文章显示IP归属地" href="https://hjyl.org/wordpress-post-get-ip-location/">WordPress发布文章显示IP归属地</a></span></div>]]></content:encoded>
					
					<wfw:commentRss>https://hjyl.org/wordpress-post-get-ip-location/feed/</wfw:commentRss>
			<slash:comments>24</slash:comments>
		
		
			</item>
	</channel>
</rss>
