1

I know there is many solutions how to rename attachment while upload files to postid. I got correct solutions that really works fine is: https://wordpress.stackexchange.com/a/30767/86517 (I'm new and I can't comments there. So I ask this question.)

The code is:

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 );

}

But Problem is, I want this changes only for 3 custom post types. Currently my site have 6 custom post type. So Only 3 custom post type need this. There is any way to allow to run this function only for those custom post type?

Thanks

  • There is a way, but where do you have this code? – Nathan Powell Jan 9 '16 at 6:40
  • I got it. I added if ( get_post_type( $_REQUEST['post_id'] ) === 'post_type_1' OR get_post_type( $_REQUEST['post_id'] ) === 'post_type_2'){} this function and its work. – I Am Stack Jan 9 '16 at 6:45
0

I'm really sorry. I fixed my problem after few minutes I post this question. So I added my working code here for future help if anyone need this.

add_action('add_attachment', 'rename_attacment');

function rename_attacment($post_ID){
 if ( get_post_type( $_REQUEST['post_id'] ) === 'post_type_1' OR get_post_type( $_REQUEST['post_id'] ) === 'post_type_2' OR get_post_type( $_REQUEST['post_id'] ) === 'post_type_3' ){
    $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 );
 }
}

Thanks to Everyone.

  • 1
    Instead of $_REQUEST['post_id'] why not just use $post_ID? – czerspalace Jan 9 '16 at 7:13
  • sorry. only $post_ID is not work for me. Only $_REQUEST['post_id'] is work. – I Am Stack Jan 10 '16 at 5:51
  • @czerspalace $post_ID is the ID of attachment, not the current post. – Shoaib Iqbal Oct 24 '17 at 7:15
0

More simple & developer friendly code

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

    //get post type
    $post_type = get_post_type($post_ID);

    //if not in allowed post types then return
    $allowed_post_types = array('post_1','post_2','post_3');
    if(!in_array($post_type,$allowed_post_types)){
      return;
    }

        $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 );

    }
  • Unfortunately file name not update or rename this method. I also tried to use only $post_ID instead of $_REQUEST['post_id'] . Only $_REQUEST['post_id'] method is work for me. – I Am Stack Jan 10 '16 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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