Skip to main content
2 of 4
added 2755 characters in body

throttle/limit a logged in user's http requests to specific page on a per day basis

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 try to implement basic page-view limiting for logged-in users. However, When I log in and view any wordpress page, no entry is made to the wp_usermeta table. Why is there no "countVisitsUsed" field added to my user table when I view a page in wordpress? **

    <?php
//First, check for get_current_user_id.

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


        // 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; //each user may only view a page twenty times.

        if (get_user_meta($current_user, 'countVisitsUsed', true)) {
            $countVisitsUsed = get_user_meta($current_user,'countVisitsUsed',true);
            update_user_meta($current_user, 'countVisitsUsed', $countVisitsUsed+1);
        }else{
              update_user_meta( $user_id, 'countVisitsUsed', 1, false );
              $countVisitsUsed = get_user_meta($current_user,'countVisitsUsed',true);
        }


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

            
            
            //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(); ?>
<?php
        }
    }