Skip to content

Instantly share code, notes, and snippets.

@jack-arturo
Last active February 11, 2025 20:38
Show Gist options
  • Save jack-arturo/f16dcdbcde3dad7fb36c57d5611a10c8 to your computer and use it in GitHub Desktop.
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
<?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