learndash-course-certificate-link-before
Fires before the course certificate link. Fires before the course certificate link is displayed for a specific user and course.
add_action( 'learndash-course-certificate-link-before', $callback, 10, 2 );
Description
Fires before the LearnDash course certificate link is displayed, allowing developers to hook in and modify output or add custom elements. This action passes the current course and user IDs. Utilize it to conditionally display certificate links, enqueue custom scripts for the certificate view, or add content before the link itself.
Usage
add_action( 'learndash-course-certificate-link-before', 'your_function_name', 10, 2 );
Parameters
-
$course_id(int) - Course ID.
-
$user_id(int) - User ID.
Examples
add_action( 'learndash-course-certificate-link-before', 'my_learndash_custom_certificate_logic', 10, 2 );
/**
* Example function to demonstrate the 'learndash-course-certificate-link-before' action hook.
* This function could be used to perform custom actions before the certificate link is displayed,
* such as logging information, conditionally altering the display based on user roles,
* or preparing data for the certificate.
*
* @param int $course_id The ID of the course.
* @param int $user_id The ID of the user.
*/
function my_learndash_custom_certificate_logic( $course_id, $user_id ) {
// Check if the current user has a specific role that should trigger a different action
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
// If the user is an administrator, perhaps log a message for debugging
error_log( "Administrator viewing certificate link for Course ID: {$course_id} for User ID: {$user_id}" );
}
// Example: Check if the course has a specific tag that requires a special certificate
$course_tags = wp_get_post_terms( $course_id, 'download_category' ); // Assuming 'download_category' is used for tags
if ( ! empty( $course_tags ) ) {
foreach ( $course_tags as $tag ) {
if ( $tag->slug === 'premium-course' ) {
// If the course is a premium course, we might want to do something specific
// For this example, we'll just log it. In a real scenario, you might
// conditionally display a different link or add specific data to the certificate.
error_log( "Premium course detected. Course ID: {$course_id}, User ID: {$user_id}" );
break; // No need to check other tags
}
}
}
// You could also store some meta data for later use if needed.
// For example, if you want to track when users *attempt* to access their certificate.
// update_user_meta( $user_id, 'learndash_certificate_access_attempt_' . $course_id, time() );
}
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/templates/single-ld30-course.php:63
* Fires before the course certificate link.
*
* @since 3.0.0
*
* @param int $course_id Course ID.
* @param int $user_id User ID.
*/
do_action( 'learndash-course-certificate-link-before', $course_id, $user_id );
/**
* Certificate link
*/
if ( $course_certficate_link && ! empty( $course_certficate_link ) ) :