Filter Uncanny Redemption Codes

ulc_codes_csv_filename

Filters the filename for the exported CSV of user location codes, allowing customization before download.

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

Description

Filters the filename for Uncanny Codes CSV exports. Allows modification of the default 'uncanny-codes-YYYY-MM-DD-HASH' filename for both login coupons and other code types. Use this to customize file naming conventions before download.


Usage

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

Parameters

$mode (mixed)
This parameter contains the filename for the CSV export.
$mode (mixed)
This parameter contains the filename for the CSV export.

Return Value

The filtered value.


Examples

/**
 * Modifies the default CSV filename for the 'login_coupon' mode.
 *
 * This filter allows you to customize the filename of the CSV file generated
 * for login coupons. For example, you might want to append the current user's ID
 * or a specific prefix based on the mode.
 *
 * @param string $filename The default filename.
 * @param string $mode     The current mode being processed (e.g., 'login_coupon').
 * @return string The modified filename.
 */
add_filter( 'ulc_codes_csv_filename', 'my_custom_ulc_codes_csv_filename', 10, 2 );

function my_custom_ulc_codes_csv_filename( $filename, $mode ) {
    // Only modify the filename if the mode is 'login_coupon'
    if ( 'login_coupon' === $mode ) {
        // Get the current logged-in user's display name.
        $current_user = wp_get_current_user();
        $user_display_name = sanitize_title( $current_user->display_name );

        // Append the user's sanitized display name to the filename.
        // Ensure it doesn't create an excessively long filename.
        $new_filename = $filename . '-' . $user_display_name;

        // Truncate if it gets too long (e.g., exceed 255 characters for some systems)
        $max_length = 200; // Leave some room for extension
        if ( strlen( $new_filename ) > $max_length ) {
            $new_filename = substr( $new_filename, 0, $max_length ) . md5( $new_filename ); // Append a hash to maintain uniqueness
        }

        return $new_filename;
    }

    // Return the original filename if the mode is not 'login_coupon'.
    return $filename;
}

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/boot.php:495
src/boot.php:525

public static function generate_csv( $mode ) {
		if ( 'login_coupon' === $mode ) {
			new CSV(
				array(
					'filename' => apply_filters( 'ulc_codes_csv_filename', 'uncanny-codes-' . date_i18n( 'Y-m-d', strtotime( current_time( 'mysql' ) ) ) . '-' . substr( md5( time() . $mode ), 0, 8 ), $mode ),
					'data'     => Database::get_coupons_csv( SharedFunctionality::ulc_filter_input( 'group_id' ) ),
				)
			);
		}
	}

Scroll to Top