Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Created June 10, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/e1fff3801dfcef9c6915375fec8f4d5f to your computer and use it in GitHub Desktop.
Save bappi-d-great/e1fff3801dfcef9c6915375fec8f4d5f to your computer and use it in GitHub Desktop.
Limit subsite based on page visit
<?php
/**
* Set MS_SUBSITE_MAX_HIT as limit of page visit
*/
if( ! defined( 'MS_SUBSITE_MAX_HIT' ) ) define( 'MS_SUBSITE_MAX_HIT', 5 );
add_action( 'shutdown', function() {
// Exclude main site
if( is_main_site() ) return;
// Exclude super admin visit
//if( is_super_admin() ) return;
// Exclude visit of admin dashboard
if( is_admin() ) return;
// Get existing visit count
$counter = ( int ) get_site_option( 'ms_site_counter' );
// Increase by 1
$counter++;
// Update with new counter
update_site_option( 'ms_site_counter', $counter );
} );
add_action( 'template_redirect', function() {
if( is_main_site() ) return;
// Get existing visit count
$counter = ( int ) get_site_option( 'ms_site_counter' );
if( $counter > MS_SUBSITE_MAX_HIT )
{
switch_to_blog( 1 );
wp_redirect( site_url() );
exit;
}
} );
add_action( 'admin_notices', function() {
// Get existing visit count
$counter = ( int ) get_site_option( 'ms_site_counter' );
if( $counter > MS_SUBSITE_MAX_HIT )
{
?>
<div class="notice notice-error is-dismissible">
<p>Your site visits have reached it's limit, please contact the administrator.</p>
</div>
<?php
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment