Filter uncanny-learndash-toolkit

uncanny_toolkit_not_enrolled_redirect_nocache

Filters whether to bypass the cache for the not-enrolled redirect, allowing for real-time checks.

add_filter( 'uncanny_toolkit_not_enrolled_redirect_nocache', $callback, 10, 1 );

Description

Filters whether a user not enrolled in a LearnDash course should be redirected. Developers can modify the redirect destination or prevent redirection entirely. This hook fires before the redirect logic executes, allowing early intervention. It is intended for internal Uncanny Toolkit use but can be hooked into for custom redirection behavior.


Usage

add_filter( 'uncanny_toolkit_not_enrolled_redirect_nocache', 'your_function_name', 10, 1 );

Parameters

$post (mixed)
This parameter is a boolean that determines whether the redirect should bypass the cache.

Return Value

The filtered value.


Examples

/**
 * Example function to demonstrate the uncanny_toolkit_not_enrolled_redirect_nocache filter.
 * This filter allows developers to conditionally add nocache headers when a user
 * is not enrolled in a LearnDash course and is about to be redirected.
 *
 * In this example, we'll add nocache headers if the user is NOT an administrator
 * and if the redirect target is specifically a "special" course page, implying
 * it might contain sensitive content or be part of a sensitive flow.
 *
 * @param bool   $add_nocache Whether to add nocache headers. Defaults to false.
 * @param int    $post_id     The ID of the current post (likely a LearnDash course).
 * @param int    $user_id     The ID of the current user. 0 if the user is not logged in.
 * @return bool True to add nocache headers, false otherwise.
 */
add_filter( 'uncanny_toolkit_not_enrolled_redirect_nocache', function( $add_nocache, $post_id, $user_id ) {

    // Check if the user is logged in and is not an administrator.
    // Administrators typically don't need this redirection logic applied in the same way.
    if ( $user_id && ! user_can( $user_id, 'administrator' ) ) {

        // Let's assume we have a way to identify "special" course pages.
        // For example, a custom field or a specific category.
        // Replace 'is_special_course_page' with your actual logic.
        $is_special_course_page = get_post_meta( $post_id, '_uncanny_toolkit_special_redirect_course', true );

        if ( $is_special_course_page === 'yes' ) {
            // If it's a special course and the user is not an admin, add nocache headers.
            return true;
        }
    }

    // If none of the conditions are met, return the original value (which is usually false).
    return $add_nocache;

}, 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/redirect-not-enrolled.php:232
src/classes/redirect-not-enrolled.php:249

public static function not_enrolled_redirect() {
		global $post;
		if ( is_admin() ) {
			return;
		}

		if ( is_archive() ) {
			return;
		}

		if ( ! $post instanceof WP_Post ) {
			return;
		}

		if ( 'sfwd-courses' !== $post->post_type ) {
			return;
		}

		$redirect_to          = '';
		$post_options_timeout = (array) learndash_get_setting( $post );

		if ( isset( $post_options_timeout['uo_redirect'] ) ) {
			$redirect_to = $post_options_timeout['uo_redirect'];
		}

		if ( empty( $redirect_to ) ) {
			return;
		}

		$redirect_to = do_shortcode( $redirect_to );

		//is there a user to check?
		if ( ! is_user_logged_in() ) {

			if( apply_filters( 'uncanny_toolkit_not_enrolled_redirect_nocache', false, $post->ID, 0 ) ){
				nocache_headers();
			}

			wp_safe_redirect( $redirect_to );
			exit;
		}

		$user = wp_get_current_user();
		//check for admins
		if ( user_can( $user, 'administrator' ) ) {
			return;
		}

		// check user access to course via direct enrollment and via group access. If both fails, redirect
		if ( ! sfwd_lms_has_access( $post->ID, get_current_user_id() ) && null === learndash_user_group_enrolled_to_course_from( $user->ID, $post->ID, true ) ) {

			if( apply_filters( 'uncanny_toolkit_not_enrolled_redirect_nocache', false, $post->ID, $user->ID ) ){
				nocache_headers();
			}

			// redirect them to the default place
			wp_safe_redirect( $redirect_to );
			exit;
		}
	}

Scroll to Top