Browse: Home / Snippets /

Redirect when accessing expired course

Contents


Snippet #

add_action( 'template_redirect', function( ) {
	global $learndash_post_types;

	// We only handle logged in users. 
	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();
		
		// Get the queried object.
		$q_object = get_queried_object();

		// And ensure it is a valid post and one of our post_types.
		if ( ( $q_object ) && ( is_a( $q_object, 'WP_Post' ) ) && ( in_array( $q_object->post_type, $learndash_post_types ) ) ) {
			$course_id = learndash_get_course_id( $q_object->ID );
			
			if ( !empty( $course_id ) ) {
				$expired = get_user_meta( $user_id, 'learndash_course_expired_'. $course_id, true );
				
				if ( !empty( $expired ) ) {
					
					wp_redirect( 'https://google.com' );
				}
			}
		}
	}
}, 1, 1 );