Browse: Home / Hooks /

learndash_course_join_redirect

apply_filters( 'learndash_course_join_redirect',  string $login_url,  int $course_id )

Filters URL that a user should be redirected to after joining a course.


Description #


Parameters #

$login_url

(string) Redirect URL.

$course_id

(int) Course ID.


Source #

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


Examples #

Note: Extended code example below not guaranteed, you may need to consult with a developer

 <?php
/**
 * Example usage for learndash_course_join_redirect filter.
 */
add_filter(
	'learndash_course_join_redirect',
	function( $link = '', $course_id = 0 ) {
		// The hard part with this filter is we need to parse out the 'redirect_to' part of the $link.
		$link_parsed = parse_url( $link );
		if ( ( isset( $link_parsed['query'] ) ) && ( ! empty( $link_parsed['query'] ) ) ) {
			$query_parts = explode( '&', $link_parsed['query'] );
			if ( ( is_array( $query_parts ) ) && ( ! empty( $query_parts ) ) ) {
				foreach ( $query_parts as $query_part_idx => $query_part ) {
					list( $query_part_key, $query_part_value ) = explode( '=', $query_part );
					if ( 'redirect_to' === $query_part_key ) {
						// Then once we find the 'redirect_to' part we have to build the replacement text.
						// With the replacement I'm adding a URL parameter 'step_id' which can be set to the first lesson ID.
						$query_part_value_new = $query_part_key . '=' . urlencode( add_query_arg( 'step_id', '655', urldecode( $query_part_value ) ) );
						$query_part_value_old = $query_part_key . '=' . $query_part_value;
						$link                 = str_replace( $query_part_value, $query_part_value_new, $link );
					}
				}
			}
		}

		// Always return $link.
		return $link;
	},
	5,
	2
);
 

Changelog #

Changelog
Version Description
2.1.0 Introduced.