Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Last active March 16, 2020 18:44
Show Gist options
  • Save ronalfy/b792e45e278ad556eb6ee507ba409954 to your computer and use it in GitHub Desktop.
Save ronalfy/b792e45e278ad556eb6ee507ba409954 to your computer and use it in GitHub Desktop.
Paid Membership Pro - Lock Down URL
<?php
/**
* This recipe will redirect non members away from a page
* according to the current URL address
*
* 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/
*/
add_action( 'init', 'my_custom_redir_on_url_slug', 20 );
function my_custom_redir_on_url_slug() {
// First check if PMPro is active
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) || is_admin() ) {
return;
}
// check on custom URL if member
if ( strpos( $_SERVER['REQUEST_URI'], '/docs' ) ) {
// Redirect members that doesn't belong to certain levels
if ( ! pmpro_hasMembershipLevel( array( '9', '1', '4', '2', '5', '6', '7' ) ) ) {
wp_safe_redirect( home_url( '/' ) );
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment