Support » Plugin: Top 10 - Popular posts plugin for WordPress » Changes to have Top10 plugin working with W3 Total Cache

  • Resolved postoffice33



    I was working with the plugin and when I installaed (and enabled) W3 Total Cache, the counters are not updated anymore.

    This is because currently the plugin generates in the page/post html code a <script> tag that includes the file top-10-addcount.js.php (that server-side increments the counters in the mySql DB).

    Unfortunately when you enable browser caching tags, the browser will NOT do this request if the page has been already visited (and cache is not expired).

    The solution to this is to do an AJAX call AND to add a random parameter value in order to have always a different URL, so avoid that the browser cache (again!) the same.

    So I changed the file top-10.php at line 75:

    $output = '<script>jQuery.ajax("' .$tptn_url. '/top-10-addcount.js.php?top_ten_id=' .$id. '&top10_rnd=" + (new Date()).getTime() + "-" + Math.floor(Math.random()*100000));</script>';

    This will ensure that the AJAX call is always done and the server can update the counters.

    Note that the changes is applied only to global stats counter (not the daily one that I am not using).


Viewing 15 replies - 1 through 15 (of 35 total)
  • Plugin Author Ajay


    Angelo, thanks for the code. I didn’t realise the level of caching in W3 Total Cache. (I’ve been using WP Super Cache).

    Have you had a chance to test WordPress’ nonce? Or is that cached as well?

    Hey guys,

    Thanks postoffice for the fix. I’m also having an issue, which I assume is due to my use of W3 total cache.

    I’d like to confirm, though, is this the line to delete?
    ” $output = ‘<script type=”text/javascript” src=”‘.$tptn_url.’/top-10-addcount.js.php?top_ten_id=’.$id.'”></script>’;

    The one on line 75 didn’t seem right, this was from line 79. But I’m no coder so maybe I’m just dead wrong (I figured maybe the file had been updated since the writing of this thread). It hasn’t seem to have made a difference yet, though (I had mine set to a 7 day count, does that rely on the daily counter?).


    Following this thread.

    Hi guys,

    here I am posting a small snippet of the file top-10.php

    Original line:

    $output = ‘<script type=”text/javascript” src=”‘.$tptn_url.’/top-10-addcount.js.php?top_ten_id=’.$id.'”></script>’;

    to be replaced with:

    $output = ‘<script>jQuery.ajax(“‘ .$tptn_url. ‘/top-10-addcount.js.php?top_ten_id=’ .$id. ‘&top10_rnd=” + (new Date()).getTime() + “-” + Math.floor(Math.random()*100000));</script>’;

    Hope this help you.


    Thanks for clarifying, Angelo, though to be fully honest I haven’t seen any of the counters move up since I changed that line last night.

    Plugin Author Ajay


    gear, could you please post your site as well?

    Also, if you temporarily disable W3, does the counter work?

    Sure the site is

    I haven’t tried disabling it (the site is currently active), but I know it used to work (I believe before I started this counter, since I was on WP super cache before).

    Hi guys,

    gearhungry what you see i normal. When you do caching of a page, on the first request of your home page for example (url=/) the system create a static .html file in the filesystem. All the other coming requests will be served (very quickly) using this static .html file.

    it means that, using my change the counter is correctly updated, BUT the users are, by default, visualizing the ‘old’ generated page.

    You can approach this problem in different ways:
    a) Considering that the cache is, by default, re-created every time you publish a nerw article. In my example I publsh, in average, 2 articles/day so for me is enough.

    b) You can set the cache expiration policy (only for the page that contains the top 10 data) more often: like each hour.

    c) You can ‘wrap’ the portion of the page containing the top 10 data with special tags (If I rememeber correctly are <!– mfunc –> and <!– /mfunc –>: this portion will be recreated at each request.
    Note that the approach (c) will impact the performance.

    In my opinion the best approach is (a) in a lot of cases.
    If you need more ‘fresh’ data, you can go for option (b) setting a expiration policy properly (but I do not recommend to be less than 15min).

    Hope this help

    PS: my site is

    Plugin Author Ajay


    gearhungry, in addition to what Angelo said above, I have been looking at the source of you’re posts, and I noticed this that the top 10 file is being loaded from

    This could possibly be the reason for the counters not updating.

    I haven’t used W3 in quite a while, but is there a way to exclude certain scripts from being cached?

    If that is a case, you might want to add /wp-content/plugins/top-10/top-10-addcount.js.php to this list

    Ok thank you, I’ve added it, I’ll report back to tell you if that fixed the issue!

    Plugin Author Ajay


    The code seems to be pointing correctly to your site now. Let us know if it works.

    Yep now it’s working, thanks for all the help!

    Plugin Author Ajay


    Glad to know! Am marking this resolved for now


    when are you planning the next release?

    I hope you will embed the changes to manage the cache (via Ajax call). It is 1 line code to change so it will be very simple for you.

    If possible can you pls add a simple config/admin options where you can disable daily stats if not needed (so avoiding doing SQL insert in the DB)? Today I have to comment manually 3 lines of code every time you do a new release…

    Thanks for your support

    Plugin Author Ajay


    I have a new release planned in the next 2 weeks (or less). The line above is a quick fix as well as the daily stats option, which I will put through in this release.

    However, am planning a bigger rework of the plugin to not use the .js.php file so as to improve performance. However, that is still a bit of a learning curve for me.

Viewing 15 replies - 1 through 15 (of 35 total)
  • The topic ‘Changes to have Top10 plugin working with W3 Total Cache’ is closed to new replies.