Wordpress displays comments location and browser type

Source: Internet
Author: User
Tags ereg explode

Show comments location

Put the following functions in your functions. php

The implementation principle is to use the IP query interfaces of Sina and Taobao to return the city to which the IP belongs.
Minor repairs the code, removes the mount point, and calls the function directly when the comment is displayed.

The code is as follows: Copy code

/**
* Use the api to obtain the <a title = "View articles related to Cities" city name
* @ Param string $ ip
* @ Return string | mixed
*/
Function wpgo_get_city ($ ip = null ){
$ Ip = null? Wpgo_get_ip (): $ ip;
$ IpApi = array (
'Http: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = ',
'Http: // ip.taobao.com/service/getIpInfo.php? Ip ='
);
 
Foreach ($ ipApi as $ k => $ api ){
$ Res = wp_remote_get ($ api. $ ip );
If (is_object ($ res) & $ k = 0 ){
Continue;
        }
If (! Empty ($ res ['body']) {
$ Return = json_decode ($ res ['body'], true );
If (! Empty ($ return ['city']) {
Return $ return ['city'];
} Elseif ($ return ['Province '] = 'Hong Kong' | $ return ['Province '] = 'Macao '){
Return $ return ['Province '];
} Else {
Return $ return ['country'];
            }
        }
    }
Return false;
}
 
/**
* Obtain the ip address of the current user.
* @ Return string
*/
Function wpgo_get_ip (){
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")){
$ Ip = getenv ("HTTP_CLIENT_IP ");
} Elseif (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")){
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
} Elseif (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown ")){
$ Ip = getenv ("REMOTE_ADDR ");
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")){
$ Ip = $ _ SERVER ['remote _ ADDR '];
    }
If ($ _ SERVER ['remote _ ADDR '] = '1970. 0.0.1' & $ ip = 'unknown '){
$ Ip = 'localhost ';
    }
Return $ ip;
}


The most important section: obtain the city field before your template outputs comments. The following is a reference to the code in my template. You can only refer to the judgment method, how can I modify it based on your template?

The code is as follows: Copy code

// The administrator replies
If ($ is_admin ){
$ City = 'reply from Postmaster ';
} Else {
// Compatible with comments without city fields
$ City = get_comment_meta ($ comment-> comment_ID, 'City _ name', true );
If (! $ City ){
$ City = wpgo_get_city ($ comment-> comment_author_IP );
// If the api can normally obtain the city information, insert it to the database.
If ($ city ){
Update_comment_meta ($ comment-> comment_ID, 'City _ name', $ city );
} Else {
// If the api information cannot be obtained due to an exception, the user-defined string is returned, which is retained the next time the comments are read.
$ City = 'Mars ';
        }
    }
$ City = "netizens from {$ city ";
}


Display reviewer browser type

Put the following code into function. php.

The code is as follows: Copy code

Function getbrowser ($ Agent)
    {
If ($ Agent = "")
$ Agent = $ _ SERVER ['http _ USER_AGENT '];
$ Browser = '';
$ Browserver = '';

If (ereg ('mozilla ', $ Agent) & ereg ('Chrome', $ Agent ))
        {
$ Temp = explode (', $ Agent );
$ Part = $ temp [2];
$ Temp = explode ('/', $ Part );
$ Browserver = $ temp [1];
$ Temp = explode ('', $ browserver );
$ Browserver = $ temp [0];
$ Browser = 'chromi ';
        }
If (ereg ('mozilla ', $ Agent) & ereg ('Firefox', $ Agent ))
        {
$ Temp = explode (', $ Agent );
$ Part = $ temp [1];
$ Temp = explode ('/', $ Part );
$ Browserver = $ temp [2];
$ Temp = explode ('', $ browserver );
$ Browserver = $ temp [0];
$ Browser = 'Firefox ';
        }
If (ereg ('mozilla ', $ Agent) & ereg ('Opera', $ Agent ))
        {
$ Temp = explode (', $ Agent );
$ Part = $ temp [1];
$ Temp = explode (')', $ Part );
$ Browserver = $ temp [1];
$ Temp = explode ('', $ browserver );
$ Browserver = $ temp [2];
$ Browser = 'Opera ';
        }
If (ereg ('mozilla ', $ Agent) & ereg ('ucbrowser', $ Agent ))
        {
$ Temp = strrchr ($ Agent ,'/');
$ Browserver = substr ($ temp, 1 );
$ Browser = 'UC ';
        }
If (ereg ('mozilla ', $ Agent) & ereg ('msie', $ Agent ))
        {
$ Temp = explode (', $ Agent );
$ Part = $ temp [1];
$ Temp = explode (';', $ Part );
$ Part = $ temp [1];
$ Temp = explode ('', $ Part );
$ Browserver = $ temp [2];
$ Browser = 'Internet Explorer ';
        }
// Add other browsers as needed
If ($ browser! = '')
        {
$ Browseinfo = $ browser. ''. $ browserver;
        }
Else
        {
$ Browseinfo = $ Agent;
        }
  
Return $ browseinfo;
    }

The above getbrowser () function returns the browser name + browser version. Call at the relevant location to display it. Finally, open wp-nodes des/comment-template under wordpress, find the function get_comment_author_link function, add the call function before the last return, and display the corresponding small icon function.

The code is as follows: Copy code
If ($ comment)
$ Ua = $ comment-> comment_agent;
Else
$ Ua = "";
$ Tmp = getbrowser ($ ua );
If ($ tmp! = ""){
$ Br = explode ('', $ tmp );
If (stristr ($ br [0], 'Chrome '))
$ Brimg = "/chrome.png ";
Elseif (stristr ($ br [0], 'Firefox '))
$ Brimg = "/firefox.png ";
Elseif (stristr ($ br [0], 'Opera '))
$ Brimg = "/opera.png ";
Elseif (stristr ($ br [0], 'internet '))
$ Brimg = "/ie.png ";
Elseif (stristr ($ br [0], 'Safari '))
$ Brimg = "/Safari.png ";
Elseif (stristr ($ br [0], 'UC '))
$ Brimg = "/ucweb.png ";
Else
$ Brimg = "/anonymouse.png ";
$ Return. = " ";
}

Well, it's done here. If you have time for the rest, you can complete other browsers. Currently, only simple identification of chrome, ie, firefox, and opera is supported.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.