Wordpress uses Ajax to calculate the number of articles browsed

Source: Internet
Author: User

However, it is really not cost-effective to use a plug-in to implement such a simple function. After a hard job, we can get a code that supports caching, and also use Ajax.

The first is the code:

The code is as follows: Copy code

Function Bing_statistics_visitors ($ cache = false ){
Global $ post;
$ Id = $ post-> ID;
If ($ cache) $ id = $ _ GET ['id'];
If ((! Is_singular ()&&! $ Cache) |! $ Id) return;
If (WP_CACHE &&! $ Cache ){
Echo '<script type = "text/javascript"> window. onload = function () {var e = null; window. XMLHttpRequest? E = new XMLHttpRequest: e = new ActiveXObject ("Microsoft. XMLHTTP "), e. open ("GET ","'. admin_url ('admin-ajax. php '). '",! 0), e. send ("action = visitors & id = '. $ id.'")} </script> ';
Return;
 }
$ Post_views = (int) get_post_meta ($ id, 'view', true );
If (! Update_post_meta ($ id, 'view', ($ post_views + 1) add_post_meta ($ id, 'view', 1, true );
}
Add_action ('WP _ head', 'Bing _ statistics_visitors ');
 
// Solve the cache problem
Function Bing_statistics_cache (){
Bing_statistics_visitors (true );
}
Add_action ('WP _ ajax_nopriv_visitors ', 'Bing _ statistics_cache ');
Add_action ('WP _ ajax_visitors ', 'Bing _ statistics_cache ');
 
// Obtain the count
Function Bing_get_views ($ get = true ){
Global $ post;
$ Views = number_format (int) get_post_meta ($ post-> ID, 'view', true ));
If ($ get) return $ views;
Echo $ views;
}

When the code enables statistics, it will automatically output a small JS for Ajax. If you have enabled caching, do not forget to delete all cached files when using my code.

Use the Bing_get_views () function to obtain or output the quantity:

The code is as follows: Copy code


// Obtain
Echo Bing_get_views ();
 
// Print directly
Bing_get_views (false );

In addition, I added a statistical code for calling php pages like js.

The document template contains the following statements:

The code is as follows: Copy code

 
<SCRIPT src = "counter. asp? ArticleId = <% = # articleId # %> "> </SCRIPT>

The counter. asp file is an asp file for data recording.
<%
'###################
#########
'By Wang Xiangchao
'###################
Dim articleId, sqlStr, hits
ArticleId = int (trim (request. querystring ("articleId ")))
SqlStr = "update articles set hits = hits 1 where articleId =" & articleId
'Add 1 to the article clicks
Conn.exe cute (sqlStr)
'Number of clicks on read articles
Hitsaskconn.exe cute ("select hits from articles where articleId =" & articleId) (0)
%>
'Print the number of clicks on the article
Document. write (<% = hits %>)

Related Article

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.