0

I have a resource on my WordPress page that I only want to show X times per day per logged in user. I'm using the twenty sixteen WordPress theme.

Currently, Guests / visitors / non logged in users are forced via redirects to authenticate to my site in order to access any content.

How do impose a http request limit / pageview limit on a per-logged-in-user per day or per hour basis? I don't want to do it by IP address, I want to do it by unique user.

Has anyone seen this done? The closet I've seen is : https://premium.wpmudev.org/forums/topic/technical-question-how-to-limit-the-number-of-page-views-for-a-specific-site-under-a-wpmu-instsall#post-384060 but is there a solution already for this? I've searched for throttle, page view, http , limit, restrict, expire, but nothing seems to be restricting content on a logged-in-user-per-day-basis.

Thank you

**UPDATE: I am overriding the default page.php file of twenty sixteen theme to implement basic page-view limiting for logged-in users. **

   <?php

//First, check for get_current_user_id.

$current_user = wp_get_current_user(); if ( 0 == $current_user->ID ) { // Logged out } else { // Logged in.

    //Then create a log of visits by adding information to a user's metadata with update_user_meta & get_user_meta. 

    $COUNT_VISITS_TOTAL_LIMIT = 20;
    $countVisitsUsed = 0;

    if (get_user_meta($current_user->ID, 'countVisitsUsed', true)) {
        $countVisitsUsed = get_user_meta($current_user->ID,'countVisitsUsed',true);
        update_user_meta($current_user->ID, 'countVisitsUsed', $countVisitsUsed+1); //increment
    }else{
          update_user_meta( $current_user->ID, 'countVisitsUsed', 1, false );
          $countVisitsUsed = get_user_meta($current_user->ID,'countVisitsUsed',true);
    }


    if ($countVisitsUsed == null || $countVisitsUsed ==false || $countVisitsUsed < $COUNT_VISITS_TOTAL_LIMIT) { //allow page load



        //start of page.php

            /**
             * The template for displaying pages
             *
             * This is the template that displays all pages by default.
             * Please note that this is the WordPress construct of pages and that
             * other "pages" on your WordPress site will use a different template.
             *
             * @package WordPress
             * @subpackage Twenty_Sixteen
             * @since Twenty Sixteen 1.0
             */

            get_header(); ?>
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
                    <?php
                    // Start the loop.
                    while ( have_posts() ) : the_post();

                        // Include the page content template.
                        get_template_part( 'template-parts/content', 'page' );

                        // If comments are open or we have at least one comment, load up the comment template.
                        if ( comments_open() || get_comments_number() ) {
                            comments_template();
                        }

                        // End of the loop.
                    endwhile;
                    ?>

                </main><!-- .site-main -->

                <?php get_sidebar( 'content-bottom' ); ?>

            </div><!-- .content-area -->

            <?php get_sidebar(); ?>
            <?php get_footer(); ?>

  • Give 'em some cookies. ;-) – MinhTri Jul 22 '16 at 2:31
0

First, check for get_current_user_id. Then create a log of visits by adding information to a user's metadata with update_user_meta & get_user_meta.

On every page hit that you care about you can run a function to determine rate/limit of usage per user.

If they break the rules, you can redirect to a page that doesn't contain that logic with wp_redirect.

  • With Jgraup's advice, I overrode my page.php file with an attempted solution. It compiles, but I have coded it incorrectly. My expectation is that a logged-in user visits a wordpress page and we count this activity by incrementing a variable in wp_usermeta. For some reason, my attempted solution and testing process do not yield any "countVisitsUsed" field in the wp_usermeta table. Any ideas? Thanks! – Peter Noges Jul 24 '16 at 6:19
  • @pdro Defining the maximum counts as a variable but then referencing it as a constant in your conditional might have something to do with it ;-) – Andy Macaulay-Brook Jul 24 '16 at 9:04
  • @AndyMacaulay-Brook thank you. It is working. – Peter Noges Jul 24 '16 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.