Filter
uncanny-automator-pro
learndash_pdf_keywords
Filters the pdf keywords. Filters PDF keywords before they are embedded into a certificate PDF.
add_filter( 'learndash_pdf_keywords', $callback, 10, 1 );
Description
Filters the keywords added to generated LearnDash PDF certificates. Developers can modify the PDF keywords string before it's applied to the TCPDF instance, allowing for custom metadata inclusion or alteration based on the certificate and its ID.
Usage
add_filter( 'learndash_pdf_keywords', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
add_filter( 'learndash_pdf_keywords', 'my_custom_learndash_pdf_keywords', 10, 3 );
/**
* Adds custom keywords to LearnDash PDF certificates.
*
* This function appends specific keywords related to the course completion
* and the user's name to the PDF metadata, enhancing searchability and
* information retrieval.
*
* @param string $pdf_keywords The original PDF keywords passed by the filter.
* @param TCPDF $pdf The TCPDF class instance.
* @param int $cert_id The ID of the certificate post.
* @return string The modified PDF keywords string.
*/
function my_custom_learndash_pdf_keywords( $pdf_keywords, $pdf, $cert_id ) {
// Get the certificate post object to access its meta data.
$certificate_post = get_post( $cert_id );
// Ensure we have a valid certificate post.
if ( ! $certificate_post || 'post' !== $certificate_post->post_type ) {
return $pdf_keywords; // Return original if not a valid certificate.
}
// Fetch the associated course ID from the certificate meta.
// Assuming LearnDash Certificate posts store the Course ID in a meta key like '_ld_certificate_course_id'.
// This meta key might vary depending on how your certificates are configured or if using a specific add-on.
$course_id = get_post_meta( $cert_id, '_ld_certificate_course_id', true );
// If a course ID is found, fetch the course title.
if ( $course_id ) {
$course = get_post( $course_id );
if ( $course && 'sfwd-courses' === $course->post_type ) {
$pdf_keywords .= ', ' . esc_attr( $course->post_title );
}
}
// Fetch the user ID associated with this certificate.
// This assumes a meta key exists that links the certificate to the user.
// Often, a certificate is generated for a specific user, and this info is stored.
// For demonstration, let's assume a meta key like '_user_id' or similar might exist.
// A more robust solution would involve fetching the user who earned the certificate.
// If this is part of a process where the user context is available:
// For example, if the $cert_args in the original source had a 'user_id':
// $user_id = $cert_args['user_id']; // Hypothetical access from original context.
// As a fallback, or if no direct user ID is available, we can try to find it if the certificate is linked to a user's completed course data.
// This is more complex and might require querying Lms Data Stores.
// For this example, we'll assume for simplicity that a user ID might be directly available or inferable.
// If not directly available, you might need to pass it through the filter or reconstruct it.
// Let's assume we can get the user ID somehow (e.g., from another meta field or passed in context).
// For this example, let's simulate getting a user ID that might be associated with the certificate.
// In a real scenario, you would fetch this from post meta or query it.
$user_id = get_post_meta( $cert_id, 'user_who_earned_this_cert_id', true ); // Example hypothetical meta key.
if ( $user_id ) {
$user_info = get_user_by( 'id', $user_id );
if ( $user_info ) {
$pdf_keywords .= ', ' . esc_attr( $user_info->display_name );
}
}
// Append a general keyword for "LearnDash Certificate".
$pdf_keywords .= ', LearnDash Certificate';
return $pdf_keywords;
}
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
uncanny-automator-pro/src/integrations/learndash/helpers/learndash-pro-helpers.php:1026
/**
* Filters the pdf keywords.
*
* @param string $pdf_keywords PDF keywords.
* @param TCPDF $pdf `TCPDF` class instance.
* @param int $cert_id Certificate post ID.
*/
$pdf->SetKeywords( apply_filters( 'learndash_pdf_keywords', $cert_args['pdf_keywords'], $pdf, $cert_args['cert_id'] ) );
// Set header data
if ( mb_strlen( $cert_args['cert_title'], 'UTF-8' ) < 42 ) {
$header_title = $cert_args['cert_title'];
} else {
$header_title = mb_substr( $cert_args['cert_title'], 0, 42, 'UTF-8' ) . '...';
}