Filter uncanny-learndash-toolkit

uo_certificate_list_shortcode

Filters the list of certificates displayed by the certificate list shortcode.

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

Description

Filters the HTML output for the certificate list shortcode before it's displayed. Allows modification of the generated certificate list, associated courses, and quiz attempt data. Useful for customizing the appearance or content of the user's certificate display.


Usage

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

Parameters

$certificate_list (mixed)
This parameter contains the HTML string representing the list of certificates that will be displayed by the shortcode.
$courses (mixed)
This parameter contains the HTML string representing the list of certificates to be displayed.
$quiz_attempts (mixed)
This parameter contains an array of course objects relevant to the certificates being displayed.

Return Value

The filtered value.


Examples

<?php

/**
 * Example of how to use the 'uo_certificate_list_shortcode' filter hook.
 * This function modifies the generated certificate list HTML to add a custom class
 * to each certificate link based on its associated course ID.
 *
 * @param string $certificate_list The current HTML string for the certificate list.
 * @param array  $courses          An array of course data.
 * @param array  $quiz_attempts    An array of quiz attempt data.
 * @return string The modified HTML string for the certificate list.
 */
function my_custom_certificate_list_modifier( $certificate_list, $courses, $quiz_attempts ) {
	// Check if the certificate_list is not empty and is a string.
	if ( ! empty( $certificate_list ) && is_string( $certificate_list ) ) {
		// This is a simplified example. In a real-world scenario, you'd likely need
		// to parse the HTML to extract relevant information like the course ID.
		// For demonstration, let's assume we can find links and add a class.
		// A more robust solution would involve better HTML parsing or accessing
		// the data before it's converted to HTML.

		// Example: find all 'a' tags and add a class based on a hypothetical course ID attribute.
		// This requires a more sophisticated HTML parser or knowledge of the structure.
		// For simplicity, we'll demonstrate a basic string replacement if we know the pattern.

		// Let's assume the $certificate_list looks something like:
		// '<a href="...">Course 1 Certificate</a><br>'
		// '<a href="...">Course 2 Certificate</a><br>'

		// If we had a way to map course IDs to certificates (which we don't directly from this hook's input),
		// we would implement that logic.

		// A more realistic scenario might involve checking the `$courses` or `$quiz_attempts`
		// to determine which certificates are being displayed and then potentially modifying
		// the `$certificate_list` based on that information.

		// For this example, let's simulate adding a unique class to each certificate link.
		// This is highly dependent on the actual structure of $certificate_list.
		// If the structure is consistent, we can use string manipulation.

		$modified_list = '';
		$links = explode( '<br>', $certificate_list ); // Assuming <br> separates entries.

		foreach ( $links as $link_html ) {
			if ( ! empty( trim( $link_html ) ) ) {
				// Attempt to find an 'a' tag within the chunk.
				if ( preg_match( '/<a(.*?)>(.*?)</a>/s', $link_html, $matches ) ) {
					$attributes = $matches[1];
					$content    = $matches[2];

					// In a real scenario, you'd extract the course_id or quiz_attempt_id
					// from the $courses or $quiz_attempts arrays based on the context of this link.
					// For demonstration, let's just add a generic class.
					// A more advanced approach would be to iterate through $courses and
					// try to find a matching link to add a specific class.

					// Let's assume for this example that we want to add a class 'my-custom-certificate-link'.
					$modified_link_html = '<a class="my-custom-certificate-link"' . $attributes . '>' . $content . '</a>';
					$modified_list .= $modified_link_html . '<br>';
				} else {
					// If it's not an 'a' tag or structure differs, append as is.
					$modified_list .= $link_html . '<br>';
				}
			}
		}

		// Ensure the final output doesn't have an extra <br> if the original did.
		if ( substr( $modified_list, -4 ) === '<br>' ) {
			$modified_list = substr( $modified_list, 0, -4 );
		}

		$certificate_list = $modified_list;
	}

	// Always return the modified or original $certificate_list.
	return $certificate_list;
}
add_filter( 'uo_certificate_list_shortcode', 'my_custom_certificate_list_modifier', 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/show-certificates-shortcode.php:274

$certificate_list .= $link . '<br>';
					}
				}
			}
		}

		$certificate_list = apply_filters_deprecated( 'certificate_list_shortcode', array( $certificate_list ), '3.6.2', 'uo_certificate_list_shortcode' );
		$certificate_list = apply_filters( 'uo_certificate_list_shortcode', $certificate_list, $courses, $quiz_attempts );

		if ( '' === $certificate_list ) {
			$certificate_list = wp_kses_post( $no_cert_message );
		}

		$shortcode_html = '';
		if( 'show' === $course_certificates ||  'show' === $quiz_certificates ||  'show' === $group_certificates ){


Scroll to Top