Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/8d767868b17426362d6859a2b41b052e to your computer and use it in GitHub Desktop.
Save kimwhite/8d767868b17426362d6859a2b41b052e to your computer and use it in GitHub Desktop.
This recipe will redirect non-members from a specific "slug" to the levels page.
<?php
/**
* This recipe will redirect non-members from a specific page to the levels 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 my_pmpro_redirect_non_members_to_another_page() {
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return;
}
/**
* To set the page to redirect non-members from change members is_page( 'select-a-challenge' )
* to the relevant page slug, e.g.
* is_page( 'my-page-slug' ),
* or page ID, e.g.
* is_page( 124 )
*/
if ( is_singular( 'listing' ) && ! pmpro_hasMembershipLevel() ) {
/**
* To redirect non-members to a different page than the PMPro levels page,
* replace pmpro_url( 'levels' )
* to target the relevant page slug, e.g.
* home_url( 'my-other-page-slug' ),
* @link https://developer.wordpress.org/reference/functions/home_url/
*
* or page ID, e.g.
* get_permalink( 82 )
* @link https://developer.wordpress.org/reference/functions/get_permalink/
*/
wp_redirect( pmpro_url( 'levels' ) );
exit;
}
}
add_action( 'template_redirect', 'my_pmpro_redirect_non_members_to_another_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment