learndash_pdf_subject
Filters the subject of the pdf. Filters the subject line for generated PDF documents, allowing customization before saving or emailing.
add_filter( 'learndash_pdf_subject', $callback, 10, 1 );
Description
Filters the subject line for generated PDF certificates. Developers can modify the default subject text, which is typically derived from the certificate's category. This hook provides access to the PDF object and the certificate ID for dynamic adjustments.
Usage
add_filter( 'learndash_pdf_subject', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
<?php
/**
* Modify the PDF subject for LearnDash certificates to include the course title.
*
* This function hooks into the 'learndash_pdf_subject' filter to prepend the
* course title to the default PDF subject.
*
* @param string $pdf_subject The original PDF subject.
* @param TCPDF $pdf The TCPDF class instance.
* @param int $cert_id The certificate post ID.
* @return string The modified PDF subject.
*/
add_filter( 'learndash_pdf_subject', function( $pdf_subject, $pdf, $cert_id ) {
// Check if we have a valid certificate ID.
if ( ! $cert_id || ! is_numeric( $cert_id ) ) {
return $pdf_subject; // Return original subject if cert_id is invalid.
}
// Get the post object for the certificate.
$certificate_post = get_post( $cert_id );
// If the certificate post exists, try to find its associated course.
if ( $certificate_post && $certificate_post->post_type === 'lms_certificate' ) {
// Get the course ID associated with this certificate.
$course_id = get_post_meta( $cert_id, 'course_id', true );
// If a course ID is found, get the course title.
if ( $course_id && is_numeric( $course_id ) ) {
$course_title = get_the_title( $course_id );
if ( ! empty( $course_title ) ) {
// Prepend the course title to the existing subject.
// We use a separator for clarity.
return $course_title . ' - ' . $pdf_subject;
}
}
}
// If no course information could be retrieved, return the original subject.
return $pdf_subject;
}, 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/includes/tcpdf-certificate-code.php:580
/**
* Filters the subject of the pdf.
*
* @param string $pdf_subject PDF subject
* @param TCPDF $pdf `TCPDF` class instance.
* @param int $cert_id Certificate post ID.
*/
$pdf->SetSubject( apply_filters( 'learndash_pdf_subject', strip_tags( get_the_category_list( ',', '', $cert_args['cert_id'] ) ), $pdf, $cert_args['cert_id'] ) );
/**
* Filters the pdf keywords.
*
* @param string $pdf_keywords PDF keywords.
* @param TCPDF $pdf `TCPDF` class instance.
* @param int $cert_id Certificate post ID.