ERI File Library
The ERI File Library plugin has some useful hooks for developers to use to further enhance its functionality.
erifl_download_file_url
TYPE ⇢ Filter
This filter allows developers to modify the URL used to download a file from the ERI File Library. The filter is triggered before the download URL is returned, enabling customization of the link structure.
Structure of $file
:
$file = [
'ID' => 3737, // The WordPress post ID for the file
'post_author' => 1, // The ID of the author of the post
'post_date' => '2025-01-01 12:00:00', // The date the post was published
'post_date_gmt' => '2025-01-01 12:00:00', // The GMT date the post was published
'post_title' => 'File Title', // The title of the file (usually the file name)
'post_name' => 'file-title', // The slug (URL-friendly name) of the post
'post_modified' => '2025-01-01 12:00:00', // The date the post was last modified
'post_modified_gmt' => '2025-01-01 12:00:00', // The GMT date the post was last modified
'url' => 'https://example.com/file.zip', // The URL to the actual file, stored in custom post meta
'description' => 'File description', // Description of the file
'download_count' => 10, // The download count for the file
'required_roles' => 'administrator,subscriber', // The required user roles for accessing the file
'required_meta_key' => 'can_download', // A meta key for additional file restrictions
'last_downloaded' => '2025-01-01 12:00:00', // The last download date/time
'last_downloaded_by' => 123, // The user ID of the last person who downloaded the file
];
Example Usage
/**
* Modify the file download URL by adding a cache busting parameter.
*
* @param string $url → The original download URL.
* @param array $file → An associative array containing the file metadata.
* @param int $user_id → The ID of the user who downloaded the file if logged in.
* @param string $user_ip → The IP address of the user who downloaded the file if logged out.
*
* @return string → The modified download URL.
*/
add_filter( 'erifl_download_file_url', function( $url, $file, $user_id, $user_ip ) {
// Add a 'clear_cache' parameter to ensure the latest version of the file is used
$url = add_query_arg( 'clear_cache', time(), $url );
return $url;
}, 10, 4 );
erifl_after_file_downloaded
TYPE ⇢ Action
This action is triggered immediately after a file is successfully downloaded. It provides full context about the file and the user who downloaded it, allowing developers to hook into the event for purposes such as analytics, logging, custom tracking, or syncing data with external systems.
Example Usage
/**
* Perform an action after a file has been downloaded.
*
* @param array $file → An associative array containing the full file and meta data.
* @param int $user_id → The ID of the user who downloaded the file if logged in.
* @param string $user_ip → The IP address of the user who downloaded the file if logged out.
*/
add_action( 'erifl_after_file_downloaded', function( $file, $user_id, $user_ip ) {
// Log info every 100 downloads
if ( $file[ 'download_count' ] > 0 && $file[ 'download_count' ] % 100 === 0 ) {
$user = ( $user_id ? ) 'ID ' . $user_id : 'with IP ' . $user_ip;
error_log( "User {$user} requested '{$file['post_title']}' on its {$file['download_count']}th download." );
}
}, 10, 3 );
erifl_user_meets_requirements
TYPE ⇢ Filter
This filter allows developers to override or extend the logic that determines whether a user has permission to download a file. It provides the current evaluation result along with the file object, user ID, and IP address for full context. This is useful for enforcing additional access rules based on custom roles, meta conditions, or external systems.
Example Usage
/**
* Override file download access requirements based on custom logic.
*
* @param bool $meets_requirements → Whether the user currently passes the access checks.
* @param array $file → An associative array containing the full file and meta data.
* @param int $user_id → The ID of the user who downloaded the file if logged in.
* @param string $user_ip → The IP address of the user who downloaded the file if logged out.
*
* @return bool → True if the user should be allowed to download, false otherwise.
*/
add_filter( 'erifl_user_meets_requirements', function( $meets_requirements, $file, $user_id, $user_ip ) {
// Block users with ID 1 from downloading any files (example only)
if ( $user_id === 1 ) {
return false;
}
return $meets_requirements;
}, 10, 4 );
erifl_classes
TYPE ⇢ Filter
This filter allows developers to modify the list of additional CSS classes applied to the outermost wrapper element for the rendered file output. It can be used to customize styling based on file metadata, shortcode attributes, or display context.
Example Usage
/**
* Add custom classes based on file format or display type.
*
* @param string $classes → The existing string of custom classes passed via the shortcode.
* @param array $file → An associative array containing the file data from get_file().
* @param string $type → The display type (e.g., 'link', 'button', 'post', etc.) passed via the shortcode.
*
* @return string → The modified string of CSS classes.
*/
add_filter( 'erifl_classes', function( $classes, $file, $type ) {
if ( $type === 'post' && !empty( $file[ 'download_count' ] ) && $file[ 'download_count' ] > 100 ) {
$classes .= ' popular-file';
}
return $classes;
}, 10, 3 );
erifl_download_icon
TYPE ⇢ Filter
Filter the icon used for file downloads.
Example Usage
/**
* Filter the icon used for file downloads.
*
* @param string $default_icon The default icon HTML.
* @param string $icon_type The requested icon type (e.g., 'logo-full', 'logo-file', 'uni', 'fa', 'cs').
* @param array $file The file data.
* @param string $ext The file extension (e.g., 'pdf', 'mp3').
*
* @return string The HTML for the icon to display.
*/
add_filter( 'erifl_download_icon', function( $default_icon, $icon_type, $file, $ext ) {
// Use a custom Font Awesome icon for PDF files
if ( $icon_type === 'fa' && $ext === 'pdf' ) {
return '<i class="fas fa-file-pdf"></i>';
}
// Fallback to the default icon for all other cases
return $default_icon;
}, 10, 4 );
erifl_downloaded_count_string
TYPE ⇢ Filter
Filter the string used to display the download count in the "post" type links.
Example Usage
/**
* Filter the string used to display the download count in the "post" type links.
*
* @param string $downloaded_string The default string for displaying the download count.
* @param int $count The number of times the file has been downloaded.
* @param array $file The file data array.
*
* @return string The string to display the download count.
*/
add_filter( 'erifl_downloaded_count_string', function( $downloaded_string, $count, $file ) {
// Customize the downloaded string for a specific file
if ( $file['post_title'] === 'Example File' ) {
return sprintf( __( 'This file has been downloaded %d times. Enjoy!', 'eri-file-library' ), $count );
}
// Use the default string for other files
return $downloaded_string;
}, 10, 3 );
erifl_format_specific_prefixes
TYPE ⇢ Filter
Filter the prefixes added to the displayed title of the link based on the file format. You can modify the prefixes applied to specific formats (e.g., audio or video) for display purposes.
Example Usage
/**
* Filter the prefixes added to the displayed title of the link based on the file format.
*
* @param array $format_prefixes The array of prefixes for different formats.
* @param array $file The file data array.
* @param string $type The type of the file (e.g., 'pdf', 'docx').
*
* @return array The array of format-specific prefixes.
*/
add_filter( 'erifl_format_specific_prefixes', function( $format_prefixes, $file, $type ) {
if ( 'pdf' === $type ) {
$format_prefixes[ 'pdf' ] = '<i class="fas fa-file-pdf"></i>';
} elseif ( 'doc' === $type || 'docx' === $type ) {
$format_prefixes[ 'docx' ] = '<i class="fas fa-file-word"></i>';
} elseif ( 'xls' === $type || 'xlsx' === $type ) {
$format_prefixes[ 'xls' ] = '<i class="fas fa-file-excel"></i>';
} elseif ( 'ppt' === $type || 'pptx' === $type ) {
$format_prefixes[ 'ppt' ] = '<i class="fas fa-file-powerpoint"></i>';
} elseif ( 'zip' === $type || 'rar' === $type ) {
$format_prefixes[ 'zip' ] = '<i class="fas fa-file-archive"></i>';
} elseif ( 'txt' === $type ) {
$format_prefixes[ 'txt' ] = '<i class="fas fa-file-alt"></i>';
}
return $format_prefixes;
}, 10, 3 );
erifl_ip_lookup_path
TYPE ⇢ Filter
Filter the link used for looking up a logged-out user's IP address on the Downloads page.
Example Usage
/**
* Filter the link used for looking up a logged-out user's IP address on the Downloads page.
*
* @param string $ip_address_link The link used to look up the IP address.
*
* @return string The modified IP address lookup link. Must include {ip} that will be replaced with the actual IP address.
*/
add_filter( 'erifl_ip_lookup_path', function( $ip_address_link ) {
return 'https://www.ip2location.com/demo/{ip}';
}, 10, 1 );
erifl_where_used_post_types
TYPE ⇢ Filter
Filter the list of post types where the shortcode can be found in the shortcode finder tool. Modify this list to control which post types the shortcode can be associated with when searching for posts or pages that include the shortcode.
Example Usage
/**
* Filter the list of post types where the shortcode is used.
*
* @param array $post_types The array of post types where the shortcode is used.
*
* @return array The modified list of post types.
*/
add_filter( 'erifl_where_used_post_types', function( $post_types ) {
// Example: Add 'custom_post_type' to the list of post types
$post_types[] = 'custom_post_type';
return $post_types;
}, 10, 1 );
erifl_post_meta
TYPE ⇢ Filter
This filter allows modification of the post metadata for a file before it is displayed. It is used to customize or add additional metadata elements such as download count, date, taxonomies, and other custom fields.
Example Usage
/**
* Filter the post metadata for a file post.
*
* @param array $meta The array of post metadata.
* @param array $file The file post data.
*
* @return array The modified array of post metadata.
*/
add_filter( 'erifl_post_meta', function( $meta, $file ) {
// Example: Add custom metadata for a file post
$meta[] = '<span class="erifl-custom-meta">Custom Meta Data</span>';
return $meta;
}, 10, 2 );