Home Forums Backend Issues (wp-admin) Detect which field is in use while uploading file


Detect which field is in use while uploading file

  • Hello!
    I feel I am really close to solving this problem but I cannot figure out how to. You can find the code below.

    I have a file upload field called “quarter_csv” and I can successfully rename the file and change its meta data while uploading it. The name is generated from the content of two other fields in the post (“quarter_year” and “quarter_quarter”).

    Everything working fine until I had to add another file upload field which, behaving as “quarter_csv”, overwrites the files previously uploaded.

    What I would like to know is how to detect which field I am using so that I can name the files including an extra variable based on the field name.

    Thanks a lot!

     * Custom file names on upload.
    function custom_upload_name( $file ) {
    	if ( isset( $_REQUEST['post_id'] ) )
    			$post_id = $_REQUEST['post_id'];
    			$oldimg = get_post_meta($post_id, "quarter_csv", true);
    			if($oldimg) {
    				wp_delete_attachment( $oldimg, true );
    			$args = array(
    				'numberposts'   => -1,
    				'post_type'	 => 'attachment',
    				'post_parent' => $post_id
    			$attachments_to_remove = get_posts( $args );
    			foreach( $attachments_to_remove as $attach )
    			wp_delete_attachment( $attach->ID, true );
    			$y = get_field('quarter_year',$post_id);
    			$q = get_field('quarter_quarter',$post_id);
    			if($y && $q) {
    				$file['name'] = $y.''.$q.''.$ext[0];
    			} else {
    				$file['name'] = $file['name'];
    		return $file;
    add_filter( 'wp_handle_upload_prefilter', 'custom_upload_name', 1); 
  • Hi @k12m

    This may not be possible with an ACF file / image field.

    There is no way to tell which field launched the uploader as this is all done with JS (PHP would not know…)

    Perhaps you could write some custom JS which listens to a click on the acf file field, when clicked, an AJAX call is sent to a PHP function which saves the field_name as a custom wp options (wp_options table).

    Then in your upload script, you could read the value from the wp_options table to find which was the last clicked field?

    It’s not pretty, but it would work

  • Hello elliot!
    Thank for your reply, that’s actually what I imagined.
    Another solution could be changing the meta info and the filename just when saving the post. I guess in that case it could be possible to retrieve the field id, isn’t it?

  • Hi @k12m

    Yes, it would be possible to do this on the save_post action!


  • Hello @elliot!

    Buddy, is there some news on this point?

    I’m working on a project that need to rename certain uploaded files, and I’m trying to do this with a WP hook (sanitize_file_name):

    function new_filename_hash($filename) {
        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($filename, $ext);
        return md5($name) . $ext;
    add_filter('sanitize_file_name', 'new_filename_hash', 10);

    It’s ok, but furthermore, I also would like to include some string based on the field name, but I get the same limitations of our friend @k12m.

    I think that your ajax suggestion could be a great solution, but I’m asking you about this just in case of a novelty 🙂

    Thank you!

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

The topic ‘Detect which field is in use while uploading file’ is closed to new replies.