Browse: Home / Snippets /

Redirect logged in user when visiting completed course

Contents


Snippet #

/**
 * LearnDash - If the logged in user has completed the course redirect them to an alternate page
 */
add_action( 'template_redirect', function() {
	if ( is_user_logged_in() ) {
		$q_object = get_queried_object();
		if ( ( $q_object ) && ( is_a( $q_object, 'WP_Post' ) ) && ( $q_object->post_type == 'sfwd-courses' ) ) {
			// Uncomment and change the 123 value if checking for specific course ID. This should be the course post ID
			//if ( $q_object->ID == 123 ) {
				$course_status = learndash_course_status( $q_object->ID, get_current_user_id() );
				if ( $course_status == esc_html__( 'Completed', 'learndash' ) ) {
					// If here the user has completed the Course so we redirect them to somewhere else
					wp_redirect( 'https://google.com );
					die();
				}
			//} // Used if checking for specific course ID
		}
	}
	
}, 1 );