Support » Plugin: WordPress Popular Posts » cheating

  • Resolved saman27

    (@saman27)


    hello

    thanks for the plugin. works great
    but there is a big problem with it
    only one visit per post per IP should be counted
    or at least there should be an option for it

    this way you will get true popular posts

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi there!

    While in your case that might be true (I’m guessing you’re running some kind of site were popularity is actually an important feature, a movie site perhaps?), this plugin is meant to be a simple way to showcase your top posts. There are more advanced analytics tools out there that do what you need (like Google Analytics.)

    With that being said, the plugin does offer some ways to implement mechanisms to prevent the views count inflating. See here for more.

    I believe popularity is an important feature for anyone who use this kind of pluigns
    Google Analytics is completely another story.

    i’m looking for a plugin which shows TOP 10 posts using shortcode (updating in real time)

    your plugin does the job but the only problem with your plugin is
    it does not have an option to count ‘unique views’

    I hope you add this option in future version!

    Plugin Author Héctor Cabrera

    (@hcabrera)

    (…) your plugin does the job but the only problem with your plugin is it does not have an option to count ‘unique views’

    I hope you add this option in future version!

    Chances are it won’t happen because of this. Performance is a great deal to me, and I’m sure it is for you as well.

    I can provide you with a code snippet you can use to detect whether a visitor has already viewed a page or not (to prevent duplicate views from the same IP on the same date) as long as you’re aware of its implications and understand you’ll be using it at your own risk (this almost sounds like a Terms of Use text haha). Would that be OK with you?

    Yes Please, I will accept that.

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Hey there!

    Alright, please add this to your theme’s functions.php file:

    /*
     * Have WordPress Popular Posts store one view 
     * per entry per IP per day to keep visitors from 
     * inflating the views count.
     */
    function wpp_unique_view( $post_ID, $views ){
    
        $ip_address = $_SERVER['REMOTE_ADDR'];
        $transient_name = 'wpp_' . $post_ID . '_' . $_SERVER['REMOTE_ADDR'];
    
        // Visitor has already seen this page
        if ( get_transient( $transient_name ) ) {
            exit();
        }
    
        // Visitor has not seen this page yet
        $now = current_time( 'timestamp' );
        $midnight = strtotime( 'midnight +1 day' );
        $expire = $midnight - $now;
    
        // Keep this record until midnight
        set_transient( $transient_name, 1, $expire );
    
    }
    add_action( 'wpp_pre_update_views', 'wpp_unique_view', 10, 2 );

    Basically, the function above keeps track of the pages visited by a given IP address. If it’s the first time this IP address has visited this page, the function will create a transient that expires at midnight so the next time the visitor checks the same page on the same day it’ll prevent WPP from updating its views count.

    By the way, if multiple users are viewing your site from the same IP address (office network, home network, etc.), all of them will be treated as one visitor.

    Additionally, you might want to install the Delete Expired Transients plugin. Although its name pretty much explains what it does, I recommend reading its description anyways to understand why you should use it.

    Thank you very much for the code

    I try to add this code to check if it work and I noticed something else

    does this plugin compatible with caching plugins (like WPFC)?

    it seems when i publish new post, WPP counting views, even post is already cached
    but after a few hours or probably a day, it stop counting that post

    • This reply was modified 2 years, 5 months ago by saman27.
    Plugin Author Héctor Cabrera

    (@hcabrera)

    does this plugin compatible with caching plugins (like WPFC)?

    Yep, it is. I’m using WP Fastest Cache as well and WPP works as intended. However, with WPFC you need some extra configurations:

    1. Go to Dashboard > WP Fastest Cache > Settings.
      • Make sure that the option “Clear cache files when a post or page is published” is enabled.
      • Go to WP Fastest Cache > Cache Timeout.
      • Click on Add New Rule.
      • Set rule as follows: All, Once every 6 Hours.
      • Hit Save to add the new rule.
      • Finally, go to WP Fastest Cache > Delete Cache and click on Delete Cache and Minified CSS/JS files to regenerate the cache.
    Plugin Author Héctor Cabrera

    (@hcabrera)

    Here’s a more modern version of the function above (requires PHP 5.3+):

    /**
     * Have WordPress Popular Posts store one view 
     * per entry per IP per day to keep visitors from 
     * inflating the views count.
     */
    function wpp_unique_view( $post_ID, $views ){
    
        $ip_address = $_SERVER['REMOTE_ADDR'];
        $transient_name = 'wpp_' . $post_ID . '_' . $_SERVER['REMOTE_ADDR'];
    
        // Visitor has already seen this page
        if ( get_transient( $transient_name ) ) {
            exit();
        }
    
        // Visitor has not seen this page yet
        $timezone = new DateTimeZone( 'GMT' . get_option( 'gmt_offset' ) );
        $now = new DateTime( 'now', $timezone );
        $midnight = new DateTime( 'tomorrow midnight -1 second', $timezone );
        $interval = $midnight->diff( $now );
    
        $expire = $interval->h * 3600 + $interval->i * 60 + $interval->s;
    
        // Keep this record until midnight
        set_transient( $transient_name, 1, $expire );
    
    }
    add_action( 'wpp_pre_update_views', 'wpp_unique_view', 10, 2 );

    Thanks again

    Deleting all cache every 6 hours is not a good idea for my website
    I have more than 30000 posts

    excluding WPP’s CSS/JS files is much better idea
    do you know what CSS/JS files I need to exclude

    other than this:
    wp-content/plugins/wordpress-popular-posts/style/wpp.css?ver=3.3.4

    Plugin Author Héctor Cabrera

    (@hcabrera)

    WPP relies on WordPress’ nonces to secure Ajax calls, and nonces expire every 24 hours.

    If you set a expiry time longer than 24 hours this is what will happen:

    1. WPP creates a security token (nonce) and uses it to update the views count of your posts and pages safely.
      • 24+ hours later your cached pages will still reflect the old security token (nonce), which has expired now.
      • WPP will attempt to use the expired security token (nonce) when updating the views count of a given post / page, and since the token is no longer valid WPP will refuse it and not update the views count.
      • And then:

        (…) but after a few hours or probably a day, it stop counting that post

    That’s why I suggested setting Cache Timeout to 6 hours. As long as it’s less or equal than a day (eg. every 8-10 hours, or even once a day) it should be fine.

    By the way, you don’t really need to exclude the wpp.css stylesheet. Feel free to remove it from the exclusion list.

    I understand now

    Expired respond :
    https://picload.org/image/dgddclgr/11.png
    —————

    but trust me deleting 30000 posts EVEN every 10 days is not good idea for my website
    I have daily more than 50K pageviews.

    there is too much pressure for server to generate cache everytime
    I would prefer to not use caching plugins to re-generate them every X hours

    anyway thank you very much for your time and help

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘cheating’ is closed to new replies.