Last active
February 11, 2025 20:38
-
-
Save jack-arturo/f16dcdbcde3dad7fb36c57d5611a10c8 to your computer and use it in GitHub Desktop.
Redirects users back to the previous page when accessing a restricted LearnDash lesson
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
<?php | |
/** | |
* Redirects users back to the previous page when accessing a restricted LearnDash lesson | |
* | |
* @param string $redirect_url The redirect URL | |
* @param int $post_id The post ID | |
* @return string The modified redirect URL | |
*/ | |
function my_learndash_redirect( $redirect_url, $post_id ) { | |
// Check if it's a LearnDash lesson | |
if ( get_post_type( $post_id ) === 'sfwd-lessons' ) { | |
// Optional, disable the generic redirect and take them back to the course: | |
// return false; | |
// Get the previous page URL | |
$previous_url = wp_get_referer(); | |
// If we have a previous URL, use it | |
if ( ! empty( $previous_url ) ) { | |
return $previous_url; | |
} | |
} | |
return $redirect_url; | |
} | |
add_filter( 'wpf_redirect_url', 'my_learndash_redirect', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment