Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:19:41+00:00 2026-06-15T14:19:41+00:00

I am trying to rename upload filenames match the Post Title. This other thread

  • 0

I am trying to rename upload filenames match the Post Title.

This other thread shows how to rename to hash:
Rename files during upload within WordPress backend

Using this code:

function make_filename_hash($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return md5($name) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);

Does anyone know the code to rename the file to match Post Title.extension?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:19:41+00:00Added an answer on June 15, 2026 at 2:19 pm

    barakadam’s answer is almost correct, just a little correction based on the comment I left below his answer.

    function new_filename($filename, $filename_raw) {
        global $post;
        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $new = $post->post_title . $ext;
        // the if is to make sure the script goes into an indefinate loop
        if( $new != $filename_raw ) {
            $new = sanitize_file_name( $new );
        }
        return $new;
    }
    add_filter('sanitize_file_name', 'new_filename', 10, 2);
    

    Explanation of code:

    Lets assume you upload a file with the original filename called picture one.jpg to a post called “My Holiday in Paris/London”.

    When you upload a file, WordPress removes special characters from the original filename using the sanitize_file_name() function.

    Right at the bottom of the function is where the filter is.

    // line 854 of wp-includes/formatting.php
    return apply_filters('sanitize_file_name', $filename, $filename_raw);
    

    At this point, $filename would be picture-one.jpg. Because we used add_filter(), our new_filename() function will be called with $filename as picture-one.jpg and $filename_raw as picture one.jpg.

    Our new_filename() function then replaces the filename with the post title with the original extension appended. If we stop here, the new filename $new would end up being My Holiday in Paris/London.jpg which all of us know is an invalid filename.

    Here is when we call the sanitize_file_name function again. Note the conditional statement there. Since $new != $filename_raw at this point, it tries to sanitize the filename again.

    sanitize_file_name() will be called and at the end of the function, $filename would be My-Holiday-in-Paris-London.jpg while $filename_raw would still be My Holiday in Paris/London.jpg. Because of the apply_filters(), our new_filename() function runs again. But this time, because $new == $filename_raw, thats where it ends.

    And My-Holiday-in-Paris-London.jpg is finally returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to rename the uploaded file with the rename-addFilter. The actual upload already
trying to rename all files in a directory to $arg[1] + number of file
I'm trying to rename several different files in bash. How to rename for example
I'm trying to rename some files, but getting a baffling error*. When I run
I am trying to rename files with a dash in the name to ensure
I am trying to rename many files in my application and need to be
I am trying to batch rename several files that are named using the following
I am trying to understand why this design decision was made with the rename()
I am trying to rename all the files present in a Windows directory using
I am trying to rename some files automatically on OSX with a python script.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.