csv_file_name
Filters the CSV filename before it's generated, allowing customization based on group and user ID.
add_filter( 'csv_file_name', $callback, 10, 3 );
Description
Filters the name of the CSV file before it's generated for download. This hook allows you to dynamically change the filename based on the provided group slug, group ID, and the current user's ID, offering customization for downloaded reports.
Usage
add_filter( 'csv_file_name', 'your_function_name', 10, 3 );
Parameters
-
$file_name(mixed) - This parameter contains the filename of the CSV file being generated, which can be modified by the filter.
-
$group_slug(mixed) - This parameter represents the name of the CSV file being generated.
-
$group_id(mixed) - This parameter is used to filter the CSV file name based on a group's slug.
Return Value
The filtered value.
Examples
add_filter(
'csv_file_name',
function ( $file_name, $group_slug, $group_id, $current_user_id ) {
// Customize the CSV file name based on the group slug, group ID, and current user.
// For example, prepend the user's role or a custom identifier.
$user = get_user_by( 'id', $current_user_id );
if ( $user && in_array( 'administrator', $user->roles, true ) ) {
// If the current user is an administrator, add 'admin-' prefix to the filename.
$file_name = 'admin-' . $file_name;
} elseif ( ! empty( $group_slug ) ) {
// If the group slug is available, append it in a more specific format if needed.
// For this example, we'll just ensure it's appended if it wasn't already.
if ( strpos( $file_name, $group_slug ) === false ) {
$file_name .= '-' . sanitize_title( $group_slug );
}
}
// Append the current date again for robustness, though it's already in the default.
$file_name .= '-' . date( 'Y-m-d' );
return $file_name;
},
10, // Priority
4 // Accepted args: $file_name, $group_slug, $group_id, $current_user_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/classes/learndash/class-learndash-modifications.php:128
src/classes/helpers/rest-api-end-points.php:1538
public function download_csv_file() {
if ( ! ulgm_filter_has_var( 'action' ) ) {
return;
}
if ( ulgm_filter_has_var( 'action' ) && 'download_seat_count' !== ulgm_filter_input( 'action' ) ) {
return;
}
if ( ! ulgm_filter_has_var( 'post' ) ) {
return;
}
if ( ! ulgm_filter_has_var( '_wpnonce' ) ) {
return;
}
if ( ! wp_verify_nonce( ulgm_filter_input( '_wpnonce' ), 'ulgm' ) ) {
return;
}
$group_id = absint( ulgm_filter_input( 'post' ) );
$users = apply_filters( 'ulgm_download_users_keys', Group_Management_Helpers::get_unused__key_users_data( $group_id ), $group_id );
$header = array(
'header' => array(
'Group',
'Key',
),
);
// open raw memory as file so no temp files needed, you might run out of memory though
$f = fopen( 'php://memory', 'w' );
$group_slug = get_post_field( 'post_name', $group_id );
$file_name = 'keys-' . $group_slug . '-' . date( 'Y-m-d' );
$file_name = apply_filters( 'csv_file_name', $file_name, $group_slug, $group_id, wp_get_current_user()->ID );
$filename = "$file_name.csv";
$delimiter = ',';
// loop over the input array
foreach ( $header as $line ) {
// generate csv lines from the inner arrays
fputcsv( $f, $line, $delimiter );
}
foreach ( $users as $line ) {
// generate csv lines from the inner arrays
fputcsv( $f, $line, $delimiter );
}
// reset the file pointer to the start of the file
rewind( $f );
// tell the browser it's going to be a csv file
header( 'Content-Type: application/csv; charset=UTF-8' );
// tell the browser we want to save it instead of displaying it
header( 'Content-Disposition: attachment;filename="' . $filename . '";' );
// make php send the generated csv lines to the browser
if ( function_exists( 'fpassthru' ) ) {
fpassthru( $f );
} else {
echo stream_get_contents( $f );
fclose( $f );
}
die();
}