Support

Account

Home Forums General Issues Setting up a new directory for a specific ACF file field

Unread

Setting up a new directory for a specific ACF file field

  • Hello,

    I have been trying unsuccessfully to change the upload path of any file uploaded via a specific ACF field.

    The field in question has a name of ‘pdf’ and a key of ‘field_67c62165a77ab’

    The upload directory I’d like to use is a folder called ‘resources’ inside the uploads folder. The directories of ‘wp-content’, ‘uploads’ and ‘resources’ all have the required write permissions of 7-7-5

    This is the code that I have tried using below. The result is that the file simply uploads in the media library using the standard month and date directory of WordPress. Maybe somebody could take a look and advise where it might be going wrong.

    function custom_acf_upload_prefilter( $file ) {
        // ACF Field key
        if ( isset($_POST['acf']['field_67c62165a77ab']) ) {
            
    // Set a custom upload directory
            $file['name'] = sanitize_file_name($file['name']);
            $upload_dir = wp_upload_dir(); // Get WordPress upload directory
            
            // Set custom folder
            $custom_dir = $upload_dir['basedir'] . '/resources/'; // Custom folder path
            
            // Create custom folder if it doesn't exist
            if (!file_exists($custom_dir)) {
                wp_mkdir_p($custom_dir);
            }
    
            // File's new path
            $file['path'] = $custom_dir . $file['name'];
            $file['url'] = $upload_dir['baseurl'] . '/resources/' . $file['name'];
            
        }
    
        return $file;
    }
    add_filter('acf/upload_prefilter', 'custom_acf_upload_prefilter');

    Something seems to be missing. Hopefully someone here can advise why the file is not getting pushed to the new directory. Thanks

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.