-
-
Save kimwhite/7e1a3a8c7468455049248e48d14808bf to your computer and use it in GitHub Desktop.
Redirect all non-members way from event pages
This file contains hidden or 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
| <? //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