learndash_certification_created
Fires after creating certificate `TCPDF` class object. Fires after a LearnDash certificate PDF object is created and before it's outputted.
add_action( 'learndash_certification_created', $callback, 10, 1 );
Description
Fires after the `TCPDF` certificate object is created. Developers can use this action to further manipulate the `TCPDF` object or certificate data before the PDF is generated. The `$pdf` object and `$cert_id` are passed for customization.
Usage
add_action( 'learndash_certification_created', 'your_function_name', 10, 1 );
Examples
add_action(
'learndash_certification_created',
function( $pdf, $cert_id ) {
// Example: Add a custom watermark to the PDF certificate.
// This function demonstrates how to access the TCPDF object and certificate ID
// to perform custom modifications to the generated PDF.
// Check if the certificate ID is valid.
if ( ! is_numeric( $cert_id ) || $cert_id <= 0 ) {
return; // Exit if certificate ID is invalid.
}
// Get some data associated with the certificate, for demonstration purposes.
$certificate_post = get_post( $cert_id );
$user_id = get_post_meta( $cert_id, 'user_id', true ); // Assuming user ID is stored as meta.
$course_id = get_post_meta( $cert_id, 'course_id', true ); // Assuming course ID is stored as meta.
// Set watermark text.
$watermark_text = 'Confidential - Course: ' . $course_id . ' - User: ' . $user_id;
// Set watermark properties.
$watermark_color = array( 128, 128, 128, 50 ); // RGBA color (Gray, 50% opacity)
$watermark_font_size = 60;
$watermark_angle = 45;
$watermark_x = $pdf->getPageWidth() / 2;
$watermark_y = $pdf->getPageHeight() / 2;
// Save the current graphic state.
$pdf->StartGraphicTransform();
// Set watermark font.
$pdf->SetFont( 'helvetica', 'B', $watermark_font_size );
// Set watermark color.
$pdf->SetTextColor( $watermark_color[0], $watermark_color[1], $watermark_color[2], $watermark_color[3] );
// Rotate and translate the watermark for placement.
$pdf->Rotate( $watermark_angle, $watermark_x, $watermark_y );
// Write the watermark text.
$pdf->Text( $watermark_x, $watermark_y, $watermark_text, false, false, true, false, 0, 0, true, false, 0, false, false, false );
// Restore the graphic state.
$pdf->StopGraphicTransform();
// You could also add other elements, like a custom header/footer,
// or dynamically add course-specific information based on $course_id.
// For example, to add a line under the title:
// $pdf->Line($pdf->GetX(), $pdf->GetY(), $pdf->GetPageWidth() - $pdf->getMargins()['right'], $pdf->GetY());
},
10, // Priority: Run after default actions.
2 // Accepted Args: This callback accepts $pdf and $cert_id.
);
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:542
*
* @param TCPDF $pdf `TCPDF` class instance.
* @param int $cert_id Certificate post ID.
*
* @since 2.4.7
*
*/
do_action( 'learndash_certification_created', $pdf, $cert_args['cert_id'] );
// Set document information
/**
* Filters the value of pdf creator.
*
* @param string $pdf_creator The name of the PDF creator.