View Categories

How to Create a Custom File Renaming Rule

Rename Media Files: Improve Your WordPress SEO ships with a set of built-in rules, but you can also add your own token and fill it with any value you compute in PHP. This guide shows how to register a custom {my_custom_rule} token and, separately, how to ignore file extensions from code. Both approaches use filters, so add the snippets to your theme’s functions.php or a small custom plugin.

Step 1: Add your token to the Filename structure #

Go to WPFactory > File Renaming in your WordPress admin. The settings page is titled File Renaming on Upload and is split into tabs. On the General Settings tab, add your token to the Filename structure field (filename_structure). For example:

{siteurl}{posttitle}{my_custom_rule}{filename}

A custom token only produces output once it is present in the Filename structure. The value comes from your filter in the next step, so the token does not need its own checkbox on the Rules tab.

Step 2: Fill the token with the frou_sanitize_file_name filter #

Use the frou_sanitize_file_name filter to set the translation for your token. In the example below the token resolves to the current user ID:

add_filter( 'frou_sanitize_file_name', function( $filename_infs ) {
    $filename_infs['structure']['translation']['my_custom_rule'] = get_current_user_id();
    return $filename_infs;
}, 20 );

Note that the array key (my_custom_rule) matches the token name without the braces. Return the full $filename_infs array so the rest of the renaming pipeline can run. Replace get_current_user_id() with whatever value you want to insert.

Ignoring file extensions from code #

If you want certain extensions left untouched during renaming, use the frou_ignored_extensions filter instead of listing them in the settings:

add_filter( 'frou_ignored_extensions', function ( $extensions ) {
    return array_merge( $extensions, array( 'txt', 'js', 'zip' ) );
} );

Files with those extensions keep their original names.

Requirements #

A custom token only takes effect when the Filename structure field includes it. If the token is missing from the structure, your filter value is never used. For the full list of available filters and actions, see the hooks and filters reference.

Need a hand? #

Our team answers every ticket on the Rename Media Files support forum.