Support

Account

Home Forums General Issues Getting the ID of the current post before wp_handle_upload_prefilter

Solved

Getting the ID of the current post before wp_handle_upload_prefilter

  • Hello,

    I am having a really hard time doing something that I thought would be really simple. I am rewriting a file name during upload using the “wp_handle_upload_prefilter” filter and I need to get the ID of the current post so that I can pull data from that post to use in the file name. If I put the ID in manually the data gets pulled just fine. However, I can’t retrieve the ID using get_the_ID() $post->ID or with a custom WP_Query called just before the filter runs.

    The filter is running with a priority of 11. It needs to do this so that the upload prefilter can do its thing before wordpress sets the file name.

    Looking at the editor page, I can see that the ID is set with javascript (see sample code below)

    acf.o = {"post_id":14}

    However, I have no idea how to get this ID inside of functions.php where I attempting to rewrite the filename.

    Here’s the relevant code in my functions.php file:

    function process_upload_prefilter() {
        
        // WP Handle Upload Prefilter:
        add_filter('wp_handle_upload_prefilter', 'rename_acf_upload', 11, 1);
    
    }
    
    function rename_acf_upload( $file ) {
        
        // NOTE: I have tried every version of get_the_ID() and also tried using global $post and custom wp_query
        // but none of them return the id
        
        $company_name = get_field('company_name');
        /* if I add the id manually on the line above, the data will be pulled in.  However, I need the post ID to be dynamic so that it will work with every post in this post type */ 
        
    	$file['name'] = $company_name . '-C.pdf';
    	    
        return $file;
        
        
    }
    
    // ACF Upload Prefilter: (report_link is the name of the field that I want to do the upload prefiltering on)
    add_filter('acf/upload_prefilter/name=report_link', 'process_upload_prefilter' );

    Thanks in advance for any help you can offer that will help me get the ID. I have spent so many hours on this with no solution to what seems like a simple issue.

    Best,
    Jack

  • After many hours of trial and error and much frustration, I was able to figure this out. Here’s the code that worked for me to retrieve the correct ID so that I could use a variable in the rewritten file name:

    function process_upload_prefilter() {
        
        // WP Handle Upload Prefilter:
        add_filter('wp_handle_upload_prefilter', 'rename_acf_upload', 11, 1);
    
    }
    
    function rename_acf_upload( $file ) {
        
        $current_post_id = $_REQUEST['post_id']; // FINALLY RETRIEVED THE ID SUCCESSFULLY!!!
        $company_name = get_field('company_name', $current_post_id);
    	$file['name'] = $company_name . '-C.pdf';
        return $file;
        
    }
    
    // ACF Upload Prefilter: (report_link is the name of the field that I want to do the upload prefiltering on)
    add_filter('acf/upload_prefilter/name=report_link', 'process_upload_prefilter' );

    Hope it helps someone else save some time in the future.

    Best,
    Jack

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

The topic ‘Getting the ID of the current post before wp_handle_upload_prefilter’ is closed to new replies.