Filter uncanny-automator-pro

automator_certificate_contents

Filters the content of a LearnDash certificate before it's generated, allowing customization of the certificate's output.

add_filter( 'automator_certificate_contents', $callback, 10, 2 );

Description

Fires to modify the certificate content before it's displayed for LearnDash courses. Developers can use this to dynamically alter text, add custom fields, or adjust the output of shortcodes like `[courseinfo]` and `[usermeta]`. It provides access to the certificate content and arguments, allowing for fine-grained control over the final certificate appearance.


Usage

add_filter( 'automator_certificate_contents', 'your_function_name', 10, 2 );

Parameters

$cert_content (mixed)
This parameter contains the current HTML content of the certificate being generated.
$args (mixed)
This parameter contains the current certificate content that is being filtered.

Return Value

The filtered value.


Examples

add_filter( 'automator_certificate_contents', 'my_custom_certificate_content_filter', 10, 2 );

/**
 * Modifies the certificate content to add custom information or change existing placeholders.
 *
 * @param mixed $cert_content The current certificate content string.
 * @param mixed $args An array or object containing additional arguments passed to the filter.
 *                    Likely to contain user information, course details, and other context.
 *
 * @return mixed The modified certificate content.
 */
function my_custom_certificate_content_filter( $cert_content, $args ) {
	// Example: Add a custom shortcode to display the user's membership level if available.
	// Assuming $args contains a 'user' object with an ID.
	if ( isset( $args['user'] ) && $args['user'] instanceof WP_User ) {
		$user_id = $args['user']->ID;
		$membership_level = get_user_meta( $user_id, 'user_membership_level', true ); // Replace with your actual meta key

		if ( ! empty( $membership_level ) ) {
			// Add a custom shortcode placeholder, e.g., [my_membership_level]
			// The Uncanny Automator's shortcode parser would need to be extended to handle this,
			// or you'd need to parse and replace it manually here if Uncanny Automator doesn't support custom shortcodes in this context.
			// For simplicity, let's assume we're directly inserting the value if the placeholder exists.

			if ( strpos( $cert_content, '[my_membership_level]' ) !== false ) {
				$cert_content = str_replace( '[my_membership_level]', esc_html( $membership_level ), $cert_content );
			}
		}
	}

	// Example: Change the format of the 'completed_on' date if a specific format is requested in $args.
	// This part demonstrates how you might interact with existing $args structure.
	if ( isset( $args['date_format_override'] ) && ! empty( $args['date_format_override'] ) ) {
		// Assuming the original code has already processed $date_format and $completion_time.
		// We'll try to re-apply the date formatting with the override.
		// This assumes $completion_time is available and $format is a variable from the original scope.
		// In a real scenario, you'd need to ensure $completion_time and $format are accessible or passed through $args.

		// For demonstration, let's assume $completion_time and $format are available or can be derived from $args.
		// If $args contains a 'completion_time' and 'date_format_setting', we'd use those.
		$completion_time_from_args = isset( $args['completion_time'] ) ? $args['completion_time'] : null;
		$format_from_args = isset( $args['date_format_setting'] ) ? $args['date_format_setting'] : null;

		if ( $completion_time_from_args && $format_from_args ) {
			// Re-apply the date formatting if the placeholder exists.
			// This is a simplified version, assuming the placeholder '[courseinfo(.*?)(completed_on)(.*?)]' is still relevant.
			$new_date_string = date_i18n( $format_from_args, $completion_time_from_args );
			$cert_content = preg_replace( '/[courseinfo(.*?)(completed_on)(.*?)]/', $new_date_string, $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

uncanny-automator-pro/src/integrations/learndash/helpers/learndash-pro-helpers.php:1201

public function generate_certificate_contents( $cert_content, $args ) {
		$user            = $args['user'];
		$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 = $this->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)(.*?)]/', '', $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 );

		return apply_filters( 'automator_certificate_contents', $cert_content, $args );
	}

Scroll to Top