Created
June 10, 2016 16:12
-
-
Save bappi-d-great/e1fff3801dfcef9c6915375fec8f4d5f to your computer and use it in GitHub Desktop.
Limit subsite based on page visit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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