One-click Query WordPress Admin Visitor IP Using Taobao IP Interface

Publish: 2016-05-18 | Modify: 2017-06-21

How to Retrieve Visitor IP Location in WordPress Backend

1. Import Jquery Library and Load JavaScript Functions

To enable IP location query in the WordPress backend, we need to import the Jquery library and load the necessary JavaScript functions. Open the admin-footer.php file located at /wp-admin/admin-footer.php and add the following code at the end, just before the closing </body> tag:

<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>

2. Add IP Query Interface File

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

<?php
    $ip = $_GET['ip']; // Get IP

    // Query IP location using Taobao IP interface
    $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 Backend Functionality

Open the class-wp-comments-list-table.php file located at wp-admin/includes/class-wp-comments-list-table.php and use a text editor to search for the keyword get_comment_author_IP. After finding the corresponding line of code:

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

Add the following code right after it:

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

Make sure to save and overwrite the original file. The encoding format should be UTF-8 to avoid any character encoding issues. Don't forget to backup the original file before making any modifications.

4. Check the Result

After completing the above steps, open the comments section in the WordPress backend. You will notice a new "Query" button. Clicking on it will display the visitor's IP location. It's that simple! If you have any questions, feel free to leave a comment.

5. Conclusion

Please note that the WordPress backend files in the wp-admin directory are numerous, so be careful not to modify the wrong file. Once again, it is essential to back up the original files before making any changes. Keep in mind that this method may be overwritten during a WordPress backend update. Consider creating a plugin to ensure the functionality remains intact.


Comments