learndash_pdf_keywords
Filters the pdf keywords. Filters the PDF keywords used for a certificate before it is generated.
add_filter( 'learndash_pdf_keywords', $callback, 10, 1 );
Description
Filters the PDF document's metadata keywords before they are set in the TCPDF instance. This allows developers to modify or add keywords based on the certificate ID and the TCPDF object itself, enabling custom SEO or searchability for generated PDF certificates.
Usage
add_filter( 'learndash_pdf_keywords', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
add_filter( 'learndash_pdf_keywords', 'my_learndash_custom_pdf_keywords', 10, 3 );
/**
* Adds custom keywords to the PDF metadata for LearnDash certificates.
*
* This function demonstrates how to hook into the 'learndash_pdf_keywords'
* filter to add specific keywords to the PDF document's metadata, enhancing
* its searchability and organization.
*
* @param string $pdf_keywords The default PDF keywords generated by LearnDash.
* @param TCPDF $pdf The TCPDF class instance.
* @param int $cert_id The post ID of the certificate.
*
* @return string The modified PDF keywords string.
*/
function my_learndash_custom_pdf_keywords( $pdf_keywords, $pdf, $cert_id ) {
// Get the certificate post object to access its details.
$certificate_post = get_post( $cert_id );
// Ensure we have a valid certificate post.
if ( ! $certificate_post instanceof WP_Post ) {
return $pdf_keywords; // Return original keywords if certificate is not found.
}
// Get the course ID associated with this certificate.
$course_id = get_post_meta( $cert_id, 'course_id', true );
// Construct our custom keywords.
$custom_keywords = array();
// Add the certificate title as a keyword.
$custom_keywords[] = $certificate_post->post_title;
// If a course ID is available, add the course title as a keyword.
if ( ! empty( $course_id ) ) {
$course_title = get_the_title( $course_id );
if ( ! empty( $course_title ) ) {
$custom_keywords[] = $course_title;
}
}
// Get the user who earned the certificate.
// This assumes a meta key like 'user_id' exists on the certificate post.
$user_id = get_post_meta( $cert_id, 'user_id', true );
if ( ! empty( $user_id ) ) {
$user_info = get_userdata( $user_id );
if ( $user_info instanceof WP_User ) {
$custom_keywords[] = $user_info->display_name;
$custom_keywords[] = $user_info->user_email;
}
}
// Combine the original keywords with our custom keywords.
// Ensure keywords are unique and separated by commas.
$all_keywords = array_unique( array_merge( explode( ',', $pdf_keywords ), $custom_keywords ) );
// Trim any whitespace from each keyword and remove empty ones.
$cleaned_keywords = array_filter( array_map( 'trim', $all_keywords ) );
// Return the final, comma-separated string of keywords.
return implode( ',', $cleaned_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
src/includes/tcpdf-certificate-code.php:589
/**
* 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' ) . '...';
}