One-Click WordPress Visitor IP Lookup Using Taobao IP API

wordpress ip lookuptaobao ip apiwordpress admin customizationvisitor ip geolocationjquery ajax wordpress
Published·Modified·

The WordPress admin comment feature records the IP address of every visitor. However, if you want to obtain the specific location of the visitor's IP, manually entering the IP into third-party IP lookup tools can be inconvenient. Therefore, Xiaoz Blog has made slight modifications to the WordPress admin panel using the Taobao IP API to enable one-click lookup of visitor IP locations.

1. Include the jQuery Library and Load JavaScript Functions

Since the query uses jQuery Ajax GET submission, it requires jQuery support. It seems the WordPress admin panel does not include the jQuery library by default, but we can manually include it. Modify the /wp-admin/admin-footer.php PHP file to include the jQuery file before the closing body tag and load the relevant functions.

<script src="https://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>
<script>
	function ipquery(ip,num) {
	var ip = ip;
	
	$.get('./ip-query.php',{ip:ip},function(data,status){
			var address = new Function('return' + data)();
			var guojia = address['data']['country'];
			var sheng = address['data']['region'];
			var shi = address['data']['city'];
			var isp = address['data']['isp'];
			var ip_add = guojia + ' ' + sheng + ' ' + shi + ' ' + isp;
			num = '#' + num;
			$(num).html(ip_add);
		});
	}
</script>

onloadjs

2. Add the IP-Query Interface File

Save the following code as ip-query.php and upload it to the /wp-admin/ directory. This interface will be used for IP queries later.

<?php
	$ip = $_GET['ip'];			// Get IP
	// Query IP location via Taobao IP API
	$get_ipquery = "http://ip.taobao.com//service/getIpInfo.php?ip=$ip";
	
	// CURL
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$get_ipquery);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_HEADER,0);

	$content = curl_exec($ch);

	curl_close($ch);
	
	
	echo $content;
?>

3. Modify WordPress Admin Functions

Locate the wp-admin/includes/class-wp-comments-list-table.php file, open it with a text editor, and search for the keyword get_comment_author_IP. Then find the following line of code:

printf( '<a href="%s">%s</a>', esc_url( $author_ip_url ), $author_ip );

After finding this line, add the following code:

$num = 'abc'.rand(1000,9999);
				echo " | <a href = 'javascript:;' onclick = 'return ipquery(\"$author_ip\",\"$num\");'>Query</a>";
				echo "<div id ='$num'></div>";

Finally, save and overwrite the source file. Note that the encoding format must be UTF-8, otherwise garbled text will occur. Please back up the source file before making modifications.

query626

4. View the Result

Finally, open the WordPress admin comment section. A "Query" button will appear. Click the query button to display the visitor's IP location. It is that simple. If you have any questions, feel free to leave a message for consultation.

ip_query

5. Summary

A few points to note: there are many files in the wp-admin directory, so be careful not to modify the wrong ones. I reiterate that you must back up the source files before making changes. The drawback of this method is that it may be overwritten after a WordPress admin panel upgrade. In the future, consider converting this into a plugin.