uo_course_grid_template
Filters the course grid template for displaying courses.
add_filter( 'uo_course_grid_template', $callback, 10, 1 );
Description
Allows developers to modify the HTML template used for displaying individual course items in the course grid. This hook provides access to the `$grid_template` variable, enabling customization of the course card's structure and content before it's rendered. Use this to inject custom elements, alter existing ones, or completely replace the default template.
Usage
add_filter( 'uo_course_grid_template', 'your_function_name', 10, 1 );
Parameters
-
$grid_template(mixed) - This parameter is the HTML template for a single course item in the grid.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the uo_course_grid_template hook.
* This example adds a custom class to the course grid template if a specific
* attribute is present in the shortcode.
*
* @param string $grid_template The path to the course grid template file.
* @return string The modified path to the course grid template file.
*/
function my_custom_course_grid_template( $grid_template ) {
// Assume we have access to shortcode attributes within this filter context.
// In a real-world scenario, you might need to access global variables or
// pass attributes differently depending on how the hook is used.
// For demonstration, let's pretend we have a global variable $my_custom_atts
// that holds the shortcode attributes.
global $my_custom_atts;
// Check if a custom attribute is set, for example, 'custom_layout'.
if ( isset( $my_custom_atts['custom_layout'] ) && 'fancy' === $my_custom_atts['custom_layout'] ) {
// If the custom attribute is set, we can either:
// 1. Modify the existing template content (more complex, requires fetching content).
// 2. Return a path to a completely different template file.
// For this example, let's assume we have a custom template file at
// 'path/to/your/theme/templates/course-grid-fancy.php'
// that you want to use when 'custom_layout' is 'fancy'.
// You would need to ensure this file exists.
$custom_template_path = trailingslashit( get_stylesheet_directory() ) . 'templates/course-grid-fancy.php';
// Check if the custom template file exists before returning its path.
if ( file_exists( $custom_template_path ) ) {
return $custom_template_path;
}
}
// If the custom condition is not met, return the original template path.
return $grid_template;
}
add_filter( 'uo_course_grid_template', 'my_custom_course_grid_template', 10, 1 );
// Example of how $my_custom_atts might be populated (this would typically happen
// within the shortcode handler that uses the course grid).
// This is just for demonstration and not part of the filter itself.
/*
function my_course_grid_shortcode( $atts ) {
$atts = shortcode_atts( array(
'custom_layout' => '', // Add your custom attribute here
// other attributes...
), $atts, 'my_course_grid' );
global $my_custom_atts;
$my_custom_atts = $atts; // Populate the global variable
// ... your existing shortcode logic that calls the function using the hook ...
// For example:
// return Uncanny_Pro_ToolkitBuilt_In_ShortcodesShow_All_Courses::display_courses($atts);
}
add_shortcode( 'my_course_grid', 'my_course_grid_shortcode' );
*/
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:835
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();
}