WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 {

share|improve this question
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 Apr 18 '11 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 Apr 18 '11 at 15:34
The comment area is limited in length, but you can edit your own question to include the extra code. – Jan Fabry Apr 18 '11 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 – schizdazzle Apr 18 '11 at 15:55

apologies for my stupidity -

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


share|improve this answer

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

share|improve this answer
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? – schizdazzle Apr 18 '11 at 14:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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