uo_show_quiz_certificate_link
Filters the certificate link for a quiz attempt before displaying it.
add_filter( 'uo_show_quiz_certificate_link', $callback, 10, 2 );
Description
Filters the HTML link for a quiz certificate. Developers can modify the link's URL, text, or even replace it entirely. The hook provides the original certificate link, the current user object, and the quiz attempt data.
Usage
add_filter( 'uo_show_quiz_certificate_link', 'your_function_name', 10, 2 );
Parameters
-
$certificate_link(mixed) - This parameter holds the generated certificate link that can be modified by the filter.
-
$quiz_attempt(mixed) - This parameter represents the currently logged-in user object, providing access to user-specific data and permissions.
Return Value
The filtered value.
Examples
<?php
/**
* Modify the certificate link for a quiz attempt.
*
* This example adds a CSS class to the certificate link if the user has a specific role.
*
* @param string $certificate_link The generated HTML link for the certificate.
* @param WP_User $current_user The current logged-in WordPress user object.
* @param object $quiz_attempt The object representing the user's quiz attempt.
* @return string The modified HTML link for the certificate.
*/
function my_custom_quiz_certificate_link( $certificate_link, $current_user, $quiz_attempt ) {
// Check if the current user has the 'administrator' role.
if ( in_array( 'administrator', $current_user->roles ) ) {
// If they do, add a custom class to the link for styling purposes.
$certificate_link = str_replace( '<a target="_blank"', '<a target="_blank" class="admin-certificate-link"', $certificate_link );
}
// You could also add logic based on $quiz_attempt, for example:
// if ( $quiz_attempt->passed === true ) {
// // Do something if the quiz was passed.
// }
return $certificate_link;
}
add_filter( 'uo_show_quiz_certificate_link', 'my_custom_quiz_certificate_link', 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:230
$certificate_id = $meta['sfwd-quiz_certificate'];
$certificate_object = get_post( $certificate_id );
if ( 'on' === $show_cert_title ) {
$certificate_title = $certificate_object->post_title;
} else {
$certificate_title = $quiz_title;
}
$link = apply_filters( 'uo_show_quiz_certificate_link', sprintf( '<a target="_blank" href="%s">%s</a>', esc_url( $certificate_link ), esc_html( $certificate_title ) ), wp_get_current_user(), $quiz_attempt );
$certificate_list .= $link . '<br>';
}
}
}
}
}