Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I'm using this question: Change Attachment FilenameChange Attachment Filename which works but it has a problem.

He renames every file with FileNameToRename and it's same image. What I want to do is instead of FileNameToRename I want to rename it to the Post Title. Is that possible ?

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 = "FileNameToRename";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}

I'm using this question: Change Attachment Filename which works but it has a problem.

He renames every file with FileNameToRename and it's same image. What I want to do is instead of FileNameToRename I want to rename it to the Post Title. Is that possible ?

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 = "FileNameToRename";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}

I'm using this question: Change Attachment Filename which works but it has a problem.

He renames every file with FileNameToRename and it's same image. What I want to do is instead of FileNameToRename I want to rename it to the Post Title. Is that possible ?

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 = "FileNameToRename";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile );

}
edited title
Link
I.Alex
  • 23
  • 1
  • 8

Rename file after title , one smilesmall problem

deleted 20 characters in body
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

Hi i seeI'm using this code herequestion:    Change attachment filenameAttachment Filename it which works for me but it havehas a problem  . He

He renames every file with "FileNameToRename" and here is the problem, he overrite every image with "FileNameToRename"FileNameToRename and it's same image.. so what iWhat I want to do is here: $newfilename = "FileNameToRename"; insteadinstead of FileNameToRename iFileNameToRename I want to rename withit to the Post Title Name , is. Is that possible ?

add_action('add_attachment', 'rename_attacment'); function rename_attacment($post_ID){

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 = "FileNameToRename";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile ); 

}

}

Thanks.

Hi i see this code here:  Change attachment filename it works for me but it have a problem  . He renames every file with "FileNameToRename" and here is the problem, he overrite every image with "FileNameToRename" and it's same image.. so what i want to do is here: $newfilename = "FileNameToRename"; instead of FileNameToRename i want to rename with Post Title Name , is that possible ?

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 = "FileNameToRename";
$newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

rename($file, $newfile);    
update_attached_file( $post_ID, $newfile );

}

Thanks.

I'm using this question:  Change Attachment Filename which works but it has a problem.

He renames every file with FileNameToRename and it's same image. What I want to do is instead of FileNameToRename I want to rename it to the Post Title. Is that possible ?

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 = "FileNameToRename";
    $newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

    rename($file, $newfile);    
    update_attached_file( $post_ID, $newfile ); 

}
Source Link
I.Alex
  • 23
  • 1
  • 8
Loading