WordPress.org

Ready to get started?Download WordPress

Forums

Top 10 - Popular posts plugin for WordPress
[resolved] Changes to have Top10 plugin working with W3 Total Cache (36 posts)

  1. postoffice33
    Member
    Posted 1 year ago #

    Hi,

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

    BYE
    Angelo

    http://wordpress.org/extend/plugins/top-10/

  2. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  3. gearhungry
    Member
    Posted 1 year ago #

    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?).

    Thanks

  4. Frederick Townes
    Member
    Posted 1 year ago #

    Following this thread.

  5. postoffice33
    Member
    Posted 1 year ago #

    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.

    BYE
    Angelo

  6. gearhungry
    Member
    Posted 1 year ago #

    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.

  7. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    gear, could you please post your site as well?

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

  8. gearhungry
    Member
    Posted 1 year ago #

    Sure the site is http://www.gearhungry.com

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

  9. postoffice33
    Member
    Posted 1 year ago #

    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
    Angelo

    PS: my site is http://best5.it

  10. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    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 cloudfront.net

    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

  11. gearhungry
    Member
    Posted 1 year ago #

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

  12. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

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

  13. gearhungry
    Member
    Posted 1 year ago #

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

  14. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Glad to know! Am marking this resolved for now

  15. postoffice33
    Member
    Posted 1 year ago #

    Ajay,

    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
    Angelo

  16. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  17. christopheran
    Member
    Posted 1 year ago #

    I just started using W3 Total Cache too and found the same thing others are experiencing here.

    I added

    /wp-content/plugins/top-10/top-10-addcount.js.php

    to the "Never cache the following pages:" on the W3TC page cache page - but when I look at my page source, it still shows the url for it as the subdomain for the cdn.

    Why would it work for gearhungry correctly but not work on my site?

    When I test site speed using pingdom, it shows the top-10 plugin as one of the slowest pages... but I suspect that's because it's cached on the cdn and not working correctly,

    Any suggestions?

    Chris

  18. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Christopheran, can you try just top-10-addcount.js.php in your "Never cache" option?

  19. christopheran
    Member
    Posted 1 year ago #

    Ajay, I'm sorry, my mistake.

    I had added the path to the page-cache not to cache section when it needed to be set in the cdn 'rejected files' section - just go confused going back and forth trying out different thing!

    Now that I have it in the right place - it is working, ie: not using the cdn for the top-10-addcount.js.php file!

    Thanks :)

    Chris

  20. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Sounds good Christopheran :)

  21. christopheran
    Member
    Posted 1 year ago #

    Ok, now I took the next step and added Cloudflare into the mix, so I'm using W3TC with MaxCDN and Cloudflare.

    It's working surprisingly well, faster than having MaxCDN by itself.

    The only thing that doesn't seem to be working is the top-10 js that displays the count.

    The count is being maintained and updated, just doesn't show on the page.

    It seems to be related to Cloudflare's Rocketloader. It puts this on the page in place of the regular src:

    <script type="text/rocketscript" data-rocketsrc="http://www.mysitename.com/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id=14093"></script>

    The rest of the js files on the page are done the same way, this is the only one that doesn't seem to be working.

    I can turn off the rocketloader site-wide and then the count is displayed. I haven't found a way to just exclude this script from the rocketloader... still researching though,

    Chris

  22. christopheran
    Member
    Posted 1 year ago #

    Ah, ok, found it.

    I had to edit top-10.php a bit.

    Added:

    data-cfasync="false"

    to

    $output = '<script data-cfasync="false" type="text/javascript" src="'.$tptn_url.'/top-10-counter.js.php?top_ten_id='.$id.'"></script>';

    That excludes it from rocketloader,

    Chris

  23. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Chris, is the rocketloader really an issue? I have the Cloudflare plugin on my site techtites.com and it gives me the rocketloader output as well, but this works perfectly.

  24. christopheran
    Member
    Posted 1 year ago #

    hmm, well, when I turned it OFF site wide using Cloudflare's admin section, the count showed, when I turned it ON, the count stopped showing.

    When I added the above code to exclude rocketloader from applying to top-10-counter.js.php it shows the count (while rocketloader is on with everything else).

    Rocketloader is on top-10-addcount.js.php and that is working ok. It's just the top-10-counter.js.php that I had to turn it off for.

    I am using MaxCDN as well, but both addcount and counter are excluded from that and loading directly from my server.

    So it looks like, in my case, it's rocketloader, but not sure why or what's different between our sites. There is still a lot of this cloudflare/maxcdn stuff I don't understand. I'm trying to though!

  25. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Chris, I think I get your point.

    Seems like the counter is missing from my site as well. I'm going to try your fix above on my site as well. Never thought about this earlier.

    Do you think it makes sense to disable rocket loader for all the scripts Top 10 adds to the page?

  26. christopheran
    Member
    Posted 1 year ago #

    The top-10-addcount.js.php I have set up so it's not on maxcdn - but it is being loaded with rocketloader and is still working, the count is incrementing ok. So I don't think it needs to be excluded from Rocketloader.

    But I'm no expert, just an experimenter :) You would know better than I if there is some other reason to exclude it.

  27. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks. Makes sense. Seems like it's working fine. I'll run my tests and hopefully all goes well.

    I'll also check with Cloudflare on what exactly is going on.

  28. christopheran
    Member
    Posted 1 year ago #

    I've been told their customer service is not very responsive - but I have never contacted them myself. Let me know what you experience - hopefully they are helpful.

  29. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    We don't really need their input actually. It's just a matter of turning of async loading.

    And, if my implementation works, we won't need to load JS anymore.

  30. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    I have added the the fixes for W3 Total Cache and the Rocketloader issue in v1.9.5 of the plugin. Do give it a spin and let me know.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic