Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Last active March 5, 2026 18:30
Show Gist options
  • Select an option

  • Save kimwhite/7e1a3a8c7468455049248e48d14808bf to your computer and use it in GitHub Desktop.

Select an option

Save kimwhite/7e1a3a8c7468455049248e48d14808bf to your computer and use it in GitHub Desktop.
Redirect all non-members way from event pages
<? //don't copy this line
/**
* Redirect non-members away from specific URLs.
*
* This example restricts the Events Calendar archive and event pages.
* Non-members visiting those URLs are redirected to the login page.
*
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function pmpro_redirect_non_members_from_events() {
// Ensure PMPro is active.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return;
}
// Allow members.
if ( pmpro_hasMembershipLevel() ) {
return;
}
// URLs that should require membership.
$restricted_urls = array(
'/events',
'/event',
);
// Current request URI.
$current_uri = strtolower( $_SERVER['REQUEST_URI'] );
foreach ( $restricted_urls as $url ) {
if ( strpos( $current_uri, strtolower( $url ) ) !== false ) {
// Prevent The Events Calendar from interfering with redirects.
if ( function_exists( 'tribe' ) ) {
remove_filter(
'wp_redirect',
array( tribe( Tribe\Events\Views\V2\Hooks::class ), 'filter_redirect_canonical' ),
10
);
}
// Redirect to the custom page, change slug here.
wp_redirect( home_url( '/your-page/' ) );
exit;
}
}
}
add_action( 'template_redirect', 'pmpro_redirect_non_members_from_events' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment