Home Forums General Issues Dynamically rename a file upon upload?


Dynamically rename a file upon upload?

  • Hi experts,
    I’m attempting to dynamically change the name of a file when a user uploads it. I currently have a repeater which consists of two fields – image (lc_photograph) and a relationship (lc_image_example). I have learned how to change the name of the file as follows, which prepends a string “ABC123-” to any image uploaded via a ‘lc_photograph’ field:

    // ACF Upload Prefilter:
    add_filter('acf/upload_prefilter/name=lc_photograph', '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'] = 'ABC123-' . $file['name'];
    	return $file;

    But is there now a way to use the postID or the post title from the associated relationship field (lc_image_example) as the string I prepend to the filename instead of a static string (ABC123-)? I don’t know how to get the post ID as a variable.

    Thanks in advance.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.