Filter uncanny-toolkit-pro

uo_course_grid_description

Filters the course description displayed in the course grid, allowing for modifications before output.

add_filter( 'uo_course_grid_description', $callback, 10, 8 );

Description

Filters the description displayed within the course grid item. Developers can modify the short description, attributes, course data, status icon, grid classes, progress percentage, completion status, and permalink before rendering. This hook provides extensive control over how individual course items appear in grid layouts.


Usage

add_filter( 'uo_course_grid_description', 'your_function_name', 10, 8 );

Parameters

$short_description (mixed)
This parameter contains the short description of the course.
$atts (mixed)
This parameter contains the short description of the course that will be displayed in the grid.
$course (mixed)
This parameter contains an array of attributes passed to the shortcode that is rendering the course grid.
$status_icon (mixed)
This parameter contains information about the specific course being displayed in the grid.
$grid_classes (mixed)
This parameter contains CSS classes that are applied to the course grid element, likely for styling purposes.
$percentage (mixed)
This parameter represents the completion percentage of the course.
$completed (mixed)
This parameter indicates whether the course has been completed by the user.
$permalink (mixed)
This parameter defines the permalink for the course, defaulting to the course's actual page permalink.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the course grid description to add a "Learn More" link for free courses.
 *
 * @param string $short_description The original short description.
 * @param array  $atts              The attributes passed to the course grid.
 * @param object $course            The current course object.
 * @param string $status_icon       The status icon HTML.
 * @param array  $grid_classes      An array of CSS classes for the grid.
 * @param int    $percentage        The completion percentage.
 * @param bool   $completed         Whether the course is completed.
 * @param string $permalink         The course permalink.
 *
 * @return string The modified short description.
 */
add_filter(
	'uo_course_grid_description',
	function( $short_description, $atts, $course, $status_icon, $grid_classes, $percentage, $completed, $permalink ) {
		// Check if the course is free.
		// This assumes there's a meta key 'sfwd-courses_course_price_type' and its value is 'free'.
		// You might need to adjust this condition based on how free courses are identified in your LearnDash setup.
		$course_options = get_post_meta( $course->ID, '_sfwd-courses', true );
		$is_free_course = false;

		if ( $course_options && isset( $course_options['sfwd-courses_course_price_type'] ) ) {
			if ( 'free' === $course_options['sfwd-courses_course_price_type'] ) {
				$is_free_course = true;
			}
		} else {
			// Fallback: If no specific price type is set, assume it might be free if no price is found.
			// This is less reliable and depends on your site's configuration.
			if ( ! $course_options || ! isset( $course_options['sfwd-courses_course_price'] ) || empty( $course_options['sfwd-courses_course_price'] ) ) {
				$is_free_course = true;
			}
		}

		if ( $is_free_course && ! empty( $permalink ) && 'javascript:;' !== $permalink ) {
			// Append a "Learn More" link if it's a free course and a valid permalink exists.
			$learn_more_text = __( 'Learn More', 'your-text-domain' );
			$short_description .= sprintf(
				' <a href="%1$s" class="course-free-learn-more">%2$s</a>',
				esc_url( $permalink ),
				esc_html( $learn_more_text )
			);
		}

		return $short_description;
	},
	10, // Priority
	8  // Accepted arguments
);

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/show-all-courses.php:831

private static function course_grid_single( $atts, $course, $status_icon, $grid_classes, $percentage = 0, $completed = false, $permalink = 'course-page' ) {
		if ( 'course-page' === $permalink ) {
			$permalink = get_permalink( $course->ID );
		} else {
			$permalink = 'javascript:;';
		}

		$hide_progress      = $atts['hide_progress'];
		$show_start_button  = $atts['start_course_button'];
		$show_resume_button = $atts['resume_course_button'];

		$course_options = get_post_meta( $course->ID, '_sfwd-courses', true );

		// Added in LD 4.1.0
		if ( function_exists( 'learndash_get_currency_symbol' ) ) {
			$currency = learndash_get_currency_symbol();
		}
		elseif ( function_exists( 'learndash_30_the_currency_symbol' ) ) {
			$currency = learndash_30_get_currency_symbol();
		} else {
			$options  = get_option( 'sfwd_cpt_options' );
			$currency = null;
			if ( ! is_null( $options ) ) {
				if ( isset( $options['modules'] ) && isset( $options['modules']['sfwd-courses_options'] ) && isset( $options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'] ) ) {
					$currency = $options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'];
				}

				if ( is_null( $currency ) ) {
					$paypal_settings = get_option( 'learndash_settings_paypal', '' );
					if ( ! empty( $paypal_settings ) ) {
						if ( ! empty( $paypal_settings['paypal_currency'] ) ) {
							$currency = $paypal_settings['paypal_currency'];
						} else {
							$currency = '$';
						}
					} else {
						$currency = '$';
					}
				}
			}
		}

		// Get the course price
		$course_price_type = ( $course_options && isset( $course_options['sfwd-courses_course_price_type'] ) ) ? $course_options['sfwd-courses_course_price_type'] : esc_html__( 'Free', 'uncanny-pro-toolkit' );
		$course_price      = ( $course_options && isset( $course_options['sfwd-courses_course_price'] ) ) ? $course_options['sfwd-courses_course_price'] : '';

		//Override Currency Symbol
		if ( ! empty( $atts['currency'] ) ) {
			$currency = $atts['currency'];
		}

		if ( absint( $course_price ) ) {
			$course_price = sprintf( esc_attr__( '%1$s %2$s', 'uncanny-pro-toolkit' ), $currency, (string) $course_price );
		}

		$short_description = '';
		if ( is_array( $course_options ) && key_exists( 'sfwd-courses_course_short_description', $course_options ) ) {
			$short_description = do_shortcode( $course_options['sfwd-courses_course_short_description'] );
		}
		$short_description_check = get_post_meta( $course->ID, 'course_short_description', true );
		if ( ! empty( $short_description_check ) ) {
			$short_description = $short_description_check;
		}
		$short_description = do_shortcode( htmlspecialchars_decode( $short_description ) );
		$short_description = apply_filters( 'uo_course_grid_description', $short_description, $atts, $course, $status_icon, $grid_classes, $percentage, $completed, $permalink );

		ob_start();
		$grid_template = self::get_template( 'course-grid.php', dirname( dirname( __FILE__ ) ) . '/src' );
		$grid_template = apply_filters( 'uo_course_grid_template', $grid_template );

		include $grid_template;

		return ob_get_clean();
	}


Scroll to Top