Support

Account

Home Forums ACF PRO Rename files upon uploading Reply To: Rename files upon uploading

  • It took quite a few hours, but I fixed it myself… so if someone needs a working example, see below.

    I added an IF clause which checks if the meta field ‘_wp_attachment_context’ is set. If so, it’s the default (basic) uploader, so I can exclude this when an image is uploaded to a gallery field.

    function add_attachment( $attachment_id ) {
        $file = $this->get_location_of_attachment_file( $attachment_id );
        if ( false !== $file ) {
            if ( 'acf-upload' == get_post_meta( $attachment_id, '_wp_attachment_context', 1 ) ) {
                $path        = pathinfo( $file );
                $newfilename = $this->getToken( 10 ); // @TODO: get from setting
                $newfile = $path[ 'dirname' ] . "/" . $newfilename . "." . $path[ 'extension' ];
    
                rename( $file, $newfile );
                update_attached_file( $attachment_id, $newfile );
            }
        }
    }

    To delete the redundant images, I check for the file name before it’s renamed and use a mask to delete all files with a certain syntax.

    NOTE: If your file name has a dot in it (and not the one before the extension), then it probably won’t work. Still need a fix for it, but I’ll add an error for it…

    function after_save_post( $post_id ) {
        if ( PostTypes::PROFILE == get_post_type( $post_id ) ) {
            
            // profile pic part removed, because nothing changed
    
            $stored_gallery = get_field( 'sd_gallery', $post_id );
            if ( is_array( $stored_gallery ) ) {
                foreach ( $stored_gallery as $image ) {
                    $file             = $this->get_location_of_attachment_file( $image[ 'ID' ] );
                    $image_is_pending = get_post_meta( $image[ 'ID' ], '_image_pending', 1 );
                    if ( $file && true == $image_is_pending ) {
                        $path        = pathinfo( $file );
                        $newfilename = $this->getToken( 10 ); // @TODO: get from setting
                        $newfile     = $path[ 'dirname' ] . "/" . $newfilename . "." . $path[ 'extension' ];
    
                        rename( $file, $newfile );
                        update_attached_file( $image[ 'ID' ], $newfile );
                        if ( class_exists( 'RegenerateThumbnails_Regenerator' ) ) {
                            $regenerator = RegenerateThumbnails_Regenerator::get_instance( $image[ 'ID' ] );
                            $regenerator->regenerate();
                        }
    
                        preg_match( '~.+(/.+)~', $file, $result );
                        $file_name  = substr( $result[ 1 ], 1 );
                        $file_noext = strstr( $file_name, '.', 1 );
                        $mask       = $file_noext . '*';
                        array_map('unlink', glob(wp_upload_dir()['path'] . "/" . $mask ) );
                    }
                }
            }
        }
    }