Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LifterLMS redirect users to login and back from email links of restricted content
<?php
/*
* Any links to restricted pages added to emails should contain a redirection parameter.
* When user lands on a restricted screen with a redirection parameter,
* they are redirected to the login screen (with the usual notice).
* On loggin in, the user is redirected back to the restricted page.
*/
add_action( 'lifterlms_content_restricted', 'llms_maybe_email_unit_redirect', 10, 1);
function llms_maybe_email_unit_redirect( $restriction ){
// do nothing in case the ?redir_login parameter isn't set
if( empty( llms_filter_input( INPUT_GET, 'login' ) ) ){
return;
}
// otherwise, filter the restriction
add_filter( 'llms_restricted_by_' . $restriction['reason'] . '_redirect', 'llms_email_unit_login_redirect', 10, 2 );
add_filter( 'llms_restricted_by_' . $restriction['reason'] . '_message', 'llms_email_unit_login_message', 10, 2 );
}
function llms_email_unit_login_message( $message, $restriction ){
return 'Please login to view <em>'. get_the_title( $restriction['content_id'] ) . '</em>.';
}
function llms_email_unit_login_redirect( $url, $restriction ){
$redirect_back_to = get_permalink( $restriction['content_id'] );
// intercept redirection by sending the dashboard url instead with a redirect paramater
$url = add_query_arg( array( 'redirect' => esc_url($redirect_back_to) ), llms_person_my_courses_url() );
return $url;
}
// hook into login form's redirection
add_filter( 'llms_student_dashboard_login_redirect', 'llms_maybe_redirect_to_unit' );
function llms_maybe_redirect_to_unit( $url ){
// since the login form's action is going to be current url, we can expect it in the querystring if redirection was set.
$unit_redirect = llms_filter_input( INPUT_GET, 'redirect' );
// do nothing in case the ?redir_login
return empty( $unit_redirect ) ? $url : $unit_redirect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.