Skip to main content
$post is not actually used. Left $post_ID rather than the suggested edit because that's what the documentation uses for this hook.
Source Link
butlerblog
  • 5.1k
  • 3
  • 26
  • 42

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}
Fix typo mentioned in comments by Avisahi.
Source Link
Sven
  • 3.7k
  • 1
  • 34
  • 48

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

Edit: Fixed typo mentioned by Avishai (for anyone copying and pasting in the future)

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

Edit: Fixed typo mentioned by Avishai (for anyone copying and pasting in the future)

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attacment''rename_attachment');
function rename_attacmentrename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

Edit: Fixed typo mentioned by Avishai (for anyone copying and pasting in the future)

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attacment');
function rename_attacment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

This will allow you to rename an attachment as soon as its uploaded:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
    
    $post = get_post($post_ID);
    $file = get_attached_file($post_ID);
    $path = pathinfo($file);
        //dirname   = File Path
        //basename  = Filename.Extension
        //extension = Extension
        //filename  = Filename
    
    $newfilename = "NEW FILE NAME HERE";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
    
    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );
    
}

Edit: Fixed typo mentioned by Avishai (for anyone copying and pasting in the future)

Bounty Ended with 25 reputation awarded by CommunityBot
Source Link
Ijaas
  • 1.3k
  • 10
  • 6
Loading