learndash_access_redirect

apply_filters( 'learndash_access_redirect',  string $link,  int $post_id )

Filters the course redirect URL after checking access.


Description #


Parameters #

$link

(string) The course URL a user is redirected to it has access.

$post_id

(int) Post ID.


Source #

File: includes/course/ld-course-functions.php


Examples #

 <?php
/**
 * Example usage for learndash_access_redirect filter.
 */
add_filter(
	'learndash_access_redirect',
	function( $link = '', $post_id = 0 ) {
		if ( ! empty( $post_id ) ) {

			// If the user is not logged in  AND the $post_id is for a lesson or topic we want to redirect to a login form.
			if ( ( ! is_user_logged_in() ) && ( in_array( get_post_type( $post_id ), array( 'sfwd-lessons', 'sfwd-topic' ) ) ) ) {
				$course_id = learndash_get_course_id( $post_id );
				if ( ! empty( $course_id ) ) {
					$step_url = learndash_get_step_permalink( $post_id, $course_id );
				} else {
					$step_url = get_permalink( $post_id );
				}

				// So now we change the $link to be the login URL then we append the 'redirect_to' parameter to send the user back to the course step
				$link = wp_login_url( $step_url );
			}
		}

		// always return $link
		return $link;

	},
	99,
	2
);