Filter uncanny-toolkit-pro

learndash_topic_available_from_text

Filters the text displayed to indicate when a LearnDash topic becomes available, allowing customization of the availability message.

add_filter( 'learndash_topic_available_from_text', $callback, 10, 3 );

Description

Filters the text displayed when a LearnDash topic is not yet available to a user. Developers can modify this message, for example, to provide more context or display different information based on the topic's availability settings and the current user. The hook provides the original content, the topic post object, and the specific availability date/time.


Usage

add_filter( 'learndash_topic_available_from_text', 'your_function_name', 10, 3 );

Parameters

$content (mixed)
This parameter contains the HTML content that is displayed when a topic is not yet available for the current user.
$post (mixed)
This parameter holds the HTML content or string that will be displayed to indicate when a topic will become available.
$topic_access_from (mixed)
This parameter contains the WordPress post object representing the topic.

Return Value

The filtered value.


Examples

add_filter( 'learndash_topic_available_from_text', 'my_custom_learndash_topic_availability_text', 10, 3 );

/**
 * Customizes the text displayed when a LearnDash topic is not yet available to the user.
 *
 * This function allows you to modify the default message shown to users
 * when a topic's availability date has not yet been reached.
 *
 * @param string $content The default HTML content for the "not available" message.
 * @param WP_Post $post    The WordPress post object for the topic.
 * @param int     $topic_access_from The Unix timestamp indicating when the topic becomes available.
 * @return string The modified HTML content for the "not available" message.
 */
function my_custom_learndash_topic_availability_text( $content, $post, $topic_access_from ) {

	// Ensure we are dealing with a topic post type.
	if ( 'sfwd-topic' !== $post->post_type ) {
		return $content;
	}

	// Get the current user.
	$user = wp_get_current_user();

	// If the user is an administrator, they should always see the content.
	if ( in_array( 'administrator', $user->roles ) ) {
		return $content;
	}

	// Get the course ID associated with the topic.
	$course_id = learndash_get_course_id( $post->ID );

	// Check if the topic has specific access restrictions defined by other plugins or custom code.
	// For this example, we'll assume we want to add a specific message if the user is enrolled
	// in a "premium" version of the course and the topic is only available after a certain date.
	// In a real-world scenario, you might check custom user meta or plugin options.
	$is_premium_user = false; // Replace with your actual logic to determine if the user is a "premium" user.
	// Example: if ( get_user_meta( $user->ID, 'is_premium_member', true ) === 'yes' ) { $is_premium_user = true; }

	if ( $is_premium_user && $topic_access_from > time() ) {
		$availability_date = learndash_adjust_date_time_display( $topic_access_from );
		$custom_message    = sprintf(
			esc_html__( 'This premium topic will be unlocked for you on %s.', 'my-text-domain' ),
			'<strong>' . $availability_date . '</strong>'
		);
		// You might want to return just the custom message or wrap it in specific HTML.
		// For this example, we'll append it to the existing content.
		$content .= '<p>' . $custom_message . '</p>';
	}

	// If no specific logic is applied, return the original content.
	return $content;
}

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/uncanny-drip-topics-by-group.php:1229
src/templates/learndash_course_topic_not_available_legacy.php:42

public static function topic_visible_after( $content, $post ) {
		if ( empty( $post->post_type ) ) {
			return $content;
		}

		$user = wp_get_current_user();
		if ( in_array( 'administrator', $user->roles ) ) {
			return $content;
		}

		$uncanny_active_classes = get_option( 'uncanny_toolkit_active_classes', '' );
		if ( ! empty( $uncanny_active_classes ) ) {
			if ( key_exists( 'uncanny_pro_toolkitGroupLeaderAccess', $uncanny_active_classes ) ) {
				$course_id         = learndash_get_course_id( $post->ID );
				$get_course_groups = learndash_get_course_groups( $course_id );
				$groups_of_leader  = learndash_get_administrators_group_ids( $user->ID );
				$matching          = array_intersect( $groups_of_leader, $get_course_groups );
				if ( in_array( 'group_leader', $user->roles ) && ! empty( $matching ) ) {
					return $content;
				}
			}
		}

		if ( 'sfwd-topic' === (string) $post->post_type ) {
			$topic_id = $post->ID;
		} elseif ( 'sfwd-quiz' === (string) $post->post_type || 'sfwd-lessons' === (string) $post->post_type ) {
			if ( 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) ) {
				$course_id = learndash_get_course_id( $post );
				$topic_id  = learndash_course_get_single_parent_step( $course_id, $post->ID );
			} else {
				$topic_id = learndash_get_setting( $post, 'topic' );
			}
		} else {
			return $content;
		}

		if ( empty( $topic_id ) ) {
			return $content;
		}
		// Compare Two of Dates and return minimum value
		$topic_access_from = self::get_topic_access_from( $topic_id, $user->ID );
		if ( __( 'Available', 'uncanny-pro-toolkit' ) === (string) $topic_access_from || empty( $topic_access_from ) ) {
			return $content;
		}


		if ( $topic_access_from > time() ) {

			$course_id = learndash_get_course_id( $topic_id );
			$content   = SFWD_LMS::get_template(
				'learndash_course_lesson_not_available',
				array(
					'user_id'                 => $user->ID,
					'course_id'               => $course_id,
					'lesson_id'               => $topic_id,
					'lesson_access_from_int'  => $topic_access_from,
					'lesson_access_from_date' => learndash_adjust_date_time_display( $topic_access_from ),
					'context'                 => 'topic',
				),
				false
			);

			if ( $content ) {
				return $content;
			} else {
				$content     = self::learndash_topic_available_from_text( $content, get_post( $topic_id ), $topic_access_from ) . '<br><br>';
				$course_link = get_permalink( $course_id );
				$content    .= '<a href="' . esc_url( $course_link ) . '">' . esc_html__( 'Return to Course Overview', 'uncanny-pro-toolkit' ) . '</a>';

				return '<div class='notavailable_message'>' . apply_filters( 'learndash_topic_available_from_text', $content, $post, $topic_access_from ) . '</div>';
			}
		}

		return $content;
	}

Scroll to Top