I'm creating a media upload section in my metabox. One of the things I want to be able to do is to rename the filename of the media that is uploaded to the same as the post title. so for example, say now I uploaded a picture called, pic001.jpg to the post: "Test Post", in the process of the upload, I want to automatically rename the filename to test_post_1.jpg

I've seen the following link:

Rename files during upload using variables

However, I'm not too sure where you would post the data to? I've also found another link that demonstrates how you can rename the file - it's something similar to this:

function make_new_filename($filename){
$info = pathinfo($filename);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    $newname = "test";
    return $newname . $ext;
add_filter('sanitize_file_name', 'make_new_filename');

So if there any idea how I would get the title posted through to the media_upload.php?

Follow up:

I have tried to send the data to the media_upload.php

jQuery('#upload_image_button_test').click(function() {
        var postTitleVal = jQuery("#title").val();
        if(postTitleVal == ''){
            jQuery("#title").after('<span class="error"> You forgot to enter the title</span>');
        } else{
        formfield = jQuery('#upload_image_test').attr('name');
        tb_show('', 'media-upload.php?type=image&TB_iframe=true;title='+postTitleVal);

        return false;

    // user inserts file into post. only run custom if user started process using the above process
    // window.send_to_editor(html) is how wp would normally handle the received data

    window.original_send_to_editor = window.send_to_editor;
    window.send_to_editor = function(html){

        if (formfield) {
            fileurl = jQuery('img',html).attr('src');




        } else {

  • Tricky, because this will only work if the post title has been set. If you upload some images before you set the post title, you can't use the title in the filename and it's not a good idea to change the filename later, because you would need to change references to the old filename too. However, if you type a title and the post draft is auto-saved, you can access that data. Good chance that it lives in the global $post variable.
    – Jan Fabry
    Commented Apr 18, 2011 at 15:30
  • Can you explain how this question is different from the question you linked to? That question also used the post title (or post slug, which is almost the same) to form the new filename. (Use @Jan in your comment and I get a notification)
    – Jan Fabry
    Commented Apr 18, 2011 at 15:34
  • The comment area is limited in length, but you can edit your own question to include the extra code.
    – Jan Fabry
    Commented Apr 18, 2011 at 15:39
  • @Jan For starters I tried the link, it doesn't seem to work anymore. Also I am not too sure where the post_id is passed onto - is it passed onto the plugin file? or is it passed onto the media_upload.php - again I've tried both, and still no joy Commented Apr 18, 2011 at 15:55

2 Answers 2


apologies for my stupidity -

The question can be answered here - and I swapped post_id for title and it works.



You need to either paste this code in your theme's functions.php file or create a plugin and paste it there.

  • If you're referring to the code I show in my question, I already know this. Perhaps I haven't been clear, what I am trying to find out is - how do I pass the title of the post (which hasn't been published yet) to the media_upload.php or to the above code? Commented Apr 18, 2011 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.