Change image name to wordpress post slug during upload

Change image name to wordpress post slug during upload

WordPress tutorial, Change image name to wordpress post slug during upload. If you want to rename files during upload and set their names to the post slug.

If you want to rename files during upload and set their names to the post slug the files are being attached to, plus some random characters (a simple incremental counter will be just fine) to make the filenames different.

Change image name to wordpress post slug during upload

Change image name to wordpress post slug during upload
Change image name to wordpress post slug during upload

In other words, if you are uploading/attaching images to the post whose page slug is “test-page-slug”, i’d like for the images to be renamed on the fly to test-page-slug-[C].[original_extension] — test-page-slug-1.jpg, test-page-slug-2.jpg etc (no matter what the original filenames were).

This is very easy. You just need to use following hook in functions.php file.

function wp_modify_uploaded_file_names($image_name) {

    // Get the parent post ID, if there is one
    if( isset($_GET['post_id']) ) {
        $post_id = $_GET['post_id'];
    } elseif( isset($_POST['post_id']) ) {
        $post_id = $_POST['post_id'];
    }

    // Only do this if we got the post ID--otherwise they're probably in
    //  the media section rather than uploading an image from a post.
    if(is_numeric($post_id)) {

        // Get the post slug
        $post_obj = get_post($post_id);
        $post_slug = $post_obj->post_name;

        // If we found a slug
        if($post_slug) {

            $random_number = rand(10000,99999);
            $image_name['name'] = $post_slug . '-' . $random_number . '.jpg';

        }

    }

    return $image_name;

}
add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);

This is very easy.
if you have pretty permalinks enabled, so I’ve added a check to make sure there is a slug before renaming the file. You’ll also want to consider checking the file type, which I haven’t done here–I’ve just assumed it’s a jpg.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

11 thoughts on “Change image name to wordpress post slug during upload”

  1. I googled for this many times till i finally found your page 🙂
    Can you please tell me why im getting this error after modifying functions.php in /wp-includes/functions.php

    Fatal error: Call to undefined function add_filter()

  2. This is an excellent function Purab. However I added a few lines to dynamically get the file extension. I also didn’t use the random number, instead I took advantage of WP 4.x function that automatically increment and adds a numerical value to the filename if a similar name is detected.

    $fileinfo = pathinfo( $image_name[‘name’] );
    $fileext = empty( $fileinfo[‘extension’] ) ? ” : ‘.’ . $fileinfo[‘extension’];

    $image_name[‘name’] = $post_slug . ‘-‘ . $fileext;

    WP 4.6.1
    PHP 5.6
    MySQL 5.6

Leave a Reply

Your email address will not be published.