Home Forums ACF PRO Rename media filename during upload


Rename media filename during upload

  • Hello all, perhaps someone with more experience can help me out with this one.

    My question is, how can I rename a media item (pdf file in my case) which is being uploaded through the ACF File upload field?

    In other words, how can I force a filename during upload (before it’s actually saved in DB), so that when the item is added to the media gallery / uploads directory, it has the forced filename (e.g., “custom_filename.pdf” instead of the default “original_filename.pdf”)?

    I searched extensively and found the acf/upload_prefilter filter. If I use this in conjunction with say WP’s upload_dir filter (for which scenario I found various working examples), all works as expected.

    But if i try to use it with wp_handle_upload_prefilter in order to rename my upload, nothing happens.

    The reason I’m using wp_handle_upload_prefilter is because as I understand it ACF relies on the native WP media uploading functionality. Is there some other approach I should try? If wp_handle_upload_prefilter is indeed the right choice for usage with acf/upload_prefilter then can someone please provide an example of the 2 working together?

    This is what my code looks like:

    add_filter('acf/upload_prefilter/name=file_upload_field_name', 'my_acf_upload_prefilter');
    function my_acf_upload_prefilter( $errors, $file, $field ) {
    	// only allow admin
    	if( !current_user_can('manage_options') ) {
    		// this returns value to the wp uploader UI
    		// if you remove the ! you can see the returned values
    		$errors[] = 'test prefilter';
    		$errors[] = print_r($_FILES,true);
    		$errors[] = $_FILES['async-upload']['name'] ;
    	add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
    	// return
    	return $errors;
    function custom_upload_filter( $file ){
    	$file['name'] = 'custom_filename';
    	return $file;

    Any pointers are greatly appreciated, thank you.

  • Solved.

    add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' ); needed to be assigned a priority of 11 or higher (since the WP default is 10).

    So the following code now allows me to rename the filename of a media file uploaded through my some_file_upload field, which is an ACF File Upload field:

    // ACF Upload Prefilter:
    add_filter('acf/upload_prefilter/name=some_file_upload', 'acf_upload_prefilter' );
    function acf_upload_prefilter() {
    	// WP Handle Upload Prefilter:
    	add_filter('wp_handle_upload_prefilter', 'rename_current_acf_upload', 11, 1);
    function rename_current_acf_upload( $file ){
    	$file['name'] = 'custom-filename-' . $file['name'];
    	return $file;
  • Thank you very much indeed!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Rename media filename during upload’ is closed to new replies.