Filter
uncanny-automator-pro
automator_pro_learndash_certificate_filename
Filters the filename for LearnDash certificates generated by Automator.
add_filter( 'automator_pro_learndash_certificate_filename', $callback, 10, 5 );
Description
Filters the filename for LearnDash certificates generated by Uncanny Automator Pro. Modify the default filename to include custom data or logic based on the user, certificate, recipe, or action. This hook allows for flexible certificate naming conventions.
Usage
add_filter( 'automator_pro_learndash_certificate_filename', 'your_function_name', 10, 5 );
Parameters
-
$file_name(mixed) - This filter allows you to modify the filename of the LearnDash certificate before it is saved.
-
$user(mixed) - This parameter is used to dynamically set the filename for the LearnDash certificate.
-
$certificate_id(mixed) - This parameter contains the WordPress user object for the user who is associated with the certificate.
-
$action_data(mixed) - This parameter contains the unique identifier of the LearnDash certificate that is being generated.
-
$recipe_id(mixed) - This parameter contains additional data related to the specific Uncanny Automator action that triggered the certificate generation.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the LearnDash certificate filename generated by Uncanny Automator Pro.
*
* This function appends the user's last name and the current date to the default filename.
*
* @param string $file_name The default generated filename.
* @param WP_User $user The user object for whom the certificate is generated.
* @param int $certificate_id The ID of the LearnDash certificate.
* @param array $action_data The data associated with the Uncanny Automator action.
* @param int $recipe_id The ID of the Uncanny Automator recipe.
*
* @return string The modified filename.
*/
add_filter( 'automator_pro_learndash_certificate_filename', function ( $file_name, $user, $certificate_id, $action_data, $recipe_id ) {
// Ensure we have a valid user object and a last name.
if ( $user instanceof WP_User && ! empty( $user->last_name ) ) {
$last_name = sanitize_title( $user->last_name ); // Sanitize for use in filename.
$date = current_time( 'Ymd' ); // Get current date in YYYYMMDD format.
// Append the last name and date to the existing filename.
$file_name = $file_name . '-' . $last_name . '-' . $date;
}
// Add a prefix to distinguish between different recipe actions if needed.
// For example, if multiple recipes might trigger certificate generation for the same user.
if ( isset( $action_data['id'] ) ) {
$file_name = 'recipe-' . $recipe_id . '-action-' . $action_data['id'] . '-' . $file_name;
}
return $file_name;
}, 10, 5 );
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/actions/ld-emailcertificate-a.php:268
// Prevent direct web access to certificate files.
file_put_contents( $save_path . '.htaccess', "Order deny,allownDeny from alln" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
file_put_contents( $save_path . 'index.php', '<?php // Silence is golden.' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
}
$user = get_user_by( 'ID', $user_id );
$file_name = 'certificate-' . $certificate_id . '-' . wp_generate_uuid4();
$file_name = apply_filters( 'automator_pro_learndash_certificate_filename', $file_name, $user, $certificate_id, $action_data, $recipe_id );
$certificate_args = array(
'certificate_post' => $certificate_id,
'save_path' => $save_path, // Add save path.
'file_name' => $file_name, // Add filename.
'user' => $user,
'orientation' => ( isset( $cert_orientation ) ) ? $cert_orientation : 'landscape',