learndash_pdf_username
Filters username of the user to be used in creating certificate PDF. Filters the user's display name for LearnDash certificate PDFs, allowing customization before generation.
add_filter( 'learndash_pdf_username', $callback, 10, 3 );
Description
This filter hook allows modification of the username displayed on LearnDash certificates. Developers can intercept and alter the user's display name before it's rendered in the PDF. This is useful for personalizing certificates with custom names or appended information. The hook provides the original username, user ID, and certificate ID for context.
Usage
add_filter( 'learndash_pdf_username', 'your_function_name', 10, 3 );
Parameters
-
$user_name(string) - User display name.
-
$user_id(int) - User ID.
-
$cert_id(int) - Certificate post ID.
Return Value
The filtered value.
Examples
add_filter( 'learndash_pdf_username', 'my_custom_learndash_pdf_username', 10, 3 );
/**
* Custom function to modify the username displayed on LearnDash certificates.
* This example appends a suffix to the username if it's a specific user.
*
* @param string $user_name The original user display name.
* @param int $user_id The ID of the user.
* @param int $cert_id The post ID of the certificate.
* @return string The modified user display name.
*/
function my_custom_learndash_pdf_username( $user_name, $user_id, $cert_id ) {
// Example: Append "- Certified" to the username for a specific user ID.
if ( 123 === $user_id ) { // Replace 123 with a real user ID for testing.
$user_name .= ' - Certified';
}
// Example: If the certificate ID is for a specific course, maybe use a different format.
// You would need to know the course IDs associated with your certificates.
// Let's assume for demonstration, certificate ID 456 is related to Course X.
if ( 456 === $cert_id ) {
// Fetch user meta for a specific course if needed.
$user_course_progress = get_user_meta( $user_id, 'course_progress_' . $cert_id, true ); // Example meta key, adjust as needed.
if ( ! empty( $user_course_progress ) && is_array( $user_course_progress ) && isset( $user_course_progress['completed_date'] ) ) {
$user_name = date( 'Y-m-d', strtotime( $user_course_progress['completed_date'] ) ) . ' - ' . $user_name;
}
}
// Always return the potentially modified username.
return $user_name;
}
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/includes/tcpdf-certificate-code.php:290
/**
* Filters username of the user to be used in creating certificate PDF.
*
* @param string $user_name User display name.
* @param int $user_id User ID.
* @param int $cert_id Certificate post ID.
*/
$learndash_pdf_username = apply_filters( 'learndash_pdf_username', $cert_args['user']->display_name, $cert_args['user_id'], $cert_args['cert_id'] );
if ( ! empty( $learndash_pdf_username ) ) {
if ( ! empty( $cert_args['pdf_title'] ) ) {
$cert_args['pdf_title'] .= " $sep ";
}
$cert_args['pdf_title'] .= $learndash_pdf_username;
}