Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwanjuki/bba051ad6e9b4d29e1dc561a00267ef6 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/bba051ad6e9b4d29e1dc561a00267ef6 to your computer and use it in GitHub Desktop.
Redirect users without membership access from LifterLMS single course pages to membership checkout.
<?php
/**
* Redirect users without membership access from LifterLMS single course pages to membership checkout.
*
* 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_lifterlms_single_course_redirect_non_members() {
// Bail if PMPro isn't active.
if ( ! function_exists( 'pmpro_has_membership_access' ) ) {
return;
}
// Bail if we're not on a Lifter LMS single course page.
if ( ! is_singular( 'course' ) ) {
return;
}
// Check the user's membership access and get required memberships.
$access = pmpro_has_membership_access( null, null, true );
// Bail if we're unable to get access information.
if ( ! is_array( $access ) ) {
return;
}
// Get access status.
$has_access = $access[0];
// Bail if the user has access.
if ( $has_access ) {
return;
}
// Get required membership level IDs.
$required_membership_levels_ids = $access[1];
// Redirect users without access to the checkout page for the first required membership level.
$checkout_url = pmpro_url( 'checkout', '?pmpro_level=' . $required_membership_levels_ids[0] );
wp_redirect( $checkout_url );
exit;
}
add_action( 'template_redirect', 'my_pmpro_lifterlms_single_course_redirect_non_members' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment