ucec_generate_preview_certificate_content
Filters the content of a certificate before it is generated, allowing modification of the certificate's data.
add_filter( 'ucec_generate_preview_certificate_content', $callback, 10, 3 );
Description
Modify the content of a certificate preview before it's displayed. This filter allows developers to alter the generated certificate text, the user object, or any custom parameters passed to the preview generation function. It's useful for adding dynamic information or formatting the certificate content.
Usage
add_filter( 'ucec_generate_preview_certificate_content', 'your_function_name', 10, 3 );
Parameters
-
$cert_content(mixed) - This parameter contains the dynamically generated HTML content for the certificate preview.
-
$user(mixed) - This parameter contains the generated certificate content as a string, which can be modified by the filter.
-
$parameters(mixed) - This parameter provides information about the user for whom the certificate is being generated.
Return Value
The filtered value.
Examples
add_filter( 'ucec_generate_preview_certificate_content', 'my_custom_certificate_preview_content', 10, 3 );
/**
* Modifies the generated certificate preview content.
*
* This example demonstrates how to alter the certificate preview by
* adding a custom message or modifying existing placeholders.
*
* @param string $cert_content The current certificate content.
* @param int $user_id The ID of the user the certificate is for.
* @param int $course_id The ID of the course the certificate is for.
*
* @return string The modified certificate content.
*/
function my_custom_certificate_preview_content( $cert_content, $user_id, $course_id ) {
// Example 1: Add a custom message for preview
$custom_preview_message = sprintf(
__( 'This is a preview of the certificate for course ID: %d for user ID: %d. Please note that dynamic data like dates and scores are simulated.', 'your-text-domain' ),
$course_id,
$user_id
);
$cert_content = '<p style="font-style: italic; color: gray;">' . esc_html( $custom_preview_message ) . '</p>' . $cert_content;
// Example 2: Replace a specific placeholder with custom text if it exists
$cert_content = str_replace( '[course_completion_note]', 'Successfully completed the course.', $cert_content );
// Example 3: Modify the course title placeholder to include user role (if available)
// This assumes '[courseinfo course_title]' placeholder is present in $cert_content.
if ( false !== strpos( $cert_content, '[courseinfo course_title]' ) ) {
$user_data = get_userdata( $user_id );
$roles = (array) $user_data->roles;
$role_name = ! empty( $roles ) ? esc_html( ucwords( str_replace( '_', ' ', $roles[0] ) ) ) : __( 'Member', 'your-text-domain' );
$course_title_placeholder = '[courseinfo course_title]';
$replacement_title = sprintf(
'%s (%s)',
__( 'Certificate of Completion', 'your-text-domain' ),
$role_name
);
$cert_content = str_replace( $course_title_placeholder, $replacement_title, $cert_content );
}
// Always return the modified content
return $cert_content;
}
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:790
public static function generate_preview_content( $cert_content, $args ) {
$user = $args['user'];
$parameters = $args['parameters'];
$completion_time = current_time( 'timestamp' );
$format = 'F d, Y';
preg_match( '/[courseinfo(.*?)(completed_on)(.*?)]/', $cert_content, $courseinfo_match );
if ( $courseinfo_match && is_array( $courseinfo_match ) ) {
$text = $courseinfo_match[0];
$date_format = self::maybe_extract_shorcode_attributes( 'courseinfo', $text );
if ( $date_format ) {
$format = key_exists( 'format', $date_format ) ? $date_format['format'] : $format;
}
}
$cert_content = preg_replace( '/[courseinfo(.*?)(course_title)(.*?)]/', esc_attr__( 'Certificate Preview', 'uncanny-ceu' ), $cert_content );
$cert_content = preg_replace( '/[courseinfo(.*?)(completed_on)(.*?)]/', date_i18n( $format, $completion_time ), $cert_content );
$cert_content = preg_replace( '/([usermeta)/', '[usermeta user_id="' . $user->ID . '" ', $cert_content );
preg_match_all( '/[quizinfo(.+?)]/', $cert_content, $matches );
if ( $matches ) {
foreach ( $matches[0] as $quizinfo ) {
if ( strpos( $quizinfo, 'timestamp' ) ) {
$qinfo = str_replace( 'show="timestamp"', '', $quizinfo );
preg_match( '/"(.*)"/', $qinfo, $date_format );
if ( $date_format ) {
$date = date_i18n( $date_format[1], $completion_time );
} else {
$date = date_i18n( 'F d, Y', $completion_time );
}
$cert_content = str_ireplace( $quizinfo, $date, $cert_content );
}
if ( strpos( $quizinfo, 'timespent' ) ) {
$cert_content = str_ireplace( $quizinfo, '88.9', $cert_content );
}
if ( strpos( $quizinfo, 'percentage' ) ) {
$cert_content = str_ireplace( $quizinfo, '85', $cert_content );
}
if ( strpos( $quizinfo, 'points' ) ) {
$cert_content = str_ireplace( $quizinfo, '8', $cert_content );
}
if ( strpos( $quizinfo, 'total_points' ) ) {
$cert_content = str_ireplace( $quizinfo, '10', $cert_content );
}
if ( strpos( $quizinfo, 'pass' ) ) {
$cert_content = str_ireplace( $quizinfo, 'Yes', $cert_content );
}
if ( strpos( $quizinfo, 'count' ) ) {
$cert_content = str_ireplace( $quizinfo, '8', $cert_content );
}
if ( strpos( $quizinfo, 'score' ) ) {
$cert_content = str_ireplace( $quizinfo, '85', $cert_content );
}
if ( strpos( $quizinfo, 'field' ) ) {
$cert_content = str_ireplace( $quizinfo, 'Custom field', $cert_content );
}
}
}
$cert_content = preg_replace( '/[courseinfo(.*?)]/', __( 'Preview', 'uncanny-toolkit-pro' ), $cert_content );
$cert_content = preg_replace( '/[quizinfo(.*?)]/', __( 'Preview', 'uncanny-toolkit-pro' ), $cert_content );
$cert_content = apply_filters( 'ucec_generate_preview_certificate_content', $cert_content, $user->ID, $parameters['course-id'] );
return $cert_content;
}