learndash_pdf_title
Filters the title of the pdf. Filters the PDF title before it is generated, allowing customization based on the PDF object and certificate ID.
add_filter( 'learndash_pdf_title', $callback, 10, 1 );
Description
This filter hook allows developers to modify the PDF document's title before it's generated. You can change the title string passed to the hook. It provides access to the `TCPDF` object and the certificate post ID for more dynamic title generation.
Usage
add_filter( 'learndash_pdf_title', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Customizes the PDF title for LearnDash certificates.
*
* This function appends the course title to the default PDF title
* for a more informative certificate.
*
* @param string $pdf_title The default PDF title.
* @param TCPDF $pdf The TCPDF instance (not used in this example).
* @param int $cert_id The ID of the certificate post.
* @return string The modified PDF title.
*/
add_filter( 'learndash_pdf_title', function( $pdf_title, $pdf, $cert_id ) {
// Get the post object for the certificate.
$certificate_post = get_post( $cert_id );
// If the certificate post doesn't exist or doesn't have a parent course.
if ( ! $certificate_post || ! isset( $certificate_post->post_parent ) || $certificate_post->post_parent === 0 ) {
return $pdf_title; // Return the original title if no parent course.
}
// Get the post object for the parent course.
$course_post = get_post( $certificate_post->post_parent );
// If the course post doesn't exist, return the original title.
if ( ! $course_post ) {
return $pdf_title;
}
// Append the course title to the existing PDF title.
$modified_pdf_title = $pdf_title . ' - ' . $course_post->post_title;
return $modified_pdf_title;
}, 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:571
/**
* Filters the title of the pdf.
*
* @param string $pdf_title PDF title.
* @param TCPDF $pdf `TCPDF` class instance.
* @param int $cert_id Certificate post ID.
*/
$pdf->SetTitle( apply_filters( 'learndash_pdf_title', $cert_args['pdf_title'], $pdf, $cert_args['cert_id'] ) );
/**
* 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.