1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. How to offload files uploaded through Product Add-Ons Ultimate to third-party cloud storage solutions
  1. Home
  2. Knowledge Base
  3. How To Guides
  4. How to offload files uploaded through Product Add-Ons Ultimate to third-party cloud storage solutions

How to offload files uploaded through Product Add-Ons Ultimate to third-party cloud storage solutions

We’re currently implementing a method to offload all files uploaded through WooCommerce Product Add-Ons Ultimate to third-party storage solutions like Amazon S3, Dropbox, Google Drive, etc.

This means that your server does not need to get filled with user uploads. Storing them on a third-party provider can reduce your hosting costs and possibly make it easier to manage uploaded files. (You can also see this article on how to manage and clean up unwanted uploads).

Requirements

As well as Product Add-Ons Ultimate (3.26.9 and above), you’ll require a third-party storage provider account and a third-party plugin to integrate the WordPress media library with your storage provide. We have tested with an Amazon S3 account and the free version of WP Offload Media – this combination works perfectly. However, any WordPress plugin that automatically detects new files in the media library should work fine.

WP Offload Media graphic

The method below works by moving files from the default Add-Ons Ultimate directory to the media library. The WP Offload Media plugin detects a new file in the media library, offloads this to the third-party provider, and returns a new URL to Add-Ons Ultimate for the uploaded image.

Current limitations

This method is being tested at the moment and has some limitations:

The method currently moves the files at the moment they’re uploaded – so before the user adds the product to the cart and way before the user checks out. This means that there’s the possibility of files being uploaded, which are then transferred to the media library and to your third-party storage provider, without the user necessarily placing an order. Further iterations of this functionality will allow you to choose when files are transferred, ensuring that unwanted files are minimized.

Method

Implementing this method is simple:

  • Install and activate your third-party offload plugin of choice, e.g. WP Offload Media
  • Create an account at your third-party storage provider, e.g. Amazon S3
  • Follow the set-up instructions for your offload plugin. We tested WP Offload Media and Amazon S3, following these steps
WP Offload Media settings
  • Add this snippet to your site – there’s no further configuration required. (Here’s how to add a snippet)
<?php
/**
* Move uploaded images to media library and thence to third-party storage like Amazon S3 etc
*/
add_filter( 'pewc_offload_media', '__return_true' );
function demo_upload_image( $uploaded_files ) {
$moved_files = array();
$delete_files = array();
if( $uploaded_files ) {
foreach( $uploaded_files as $index=>$upload ) {
$moved_files[$index] = $upload;
// Create the full URL
$url = get_site_url() . $upload['url'];
// Ensure we don't generate all the different media sizes for this upload
add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array' );
// Move the upload to the media library
$id = media_handle_sideload( $moved_files[$index], 0 );
$moved_files[$index]['file'] = get_attached_file( $id );
$moved_files[$index]['url'] = wp_get_attachment_url( $id );
// If error storing permanently, unlink
if ( is_wp_error( $id ) ) {
// Something went wrong
@unlink( $upload['file'] );
return $uploaded_files;
} else {
// Clear the upload file and any temporary files from the original directory
$delete_files[] = $upload['file'];
$delete_files[] = $moved_files[$index]['file'];
}
}
}
if( $delete_files ) {
foreach( $delete_files as $delete_file ) {
@unlink( $delete_file );
}
}
return $moved_files;
}
function demo_move_uploads_to_media_library( $uploaded_files, $post_obj ) {
if( apply_filters( 'pewc_offload_media', false ) ) {
$uploaded_files = demo_upload_image( $uploaded_files );
}
return $uploaded_files;
}
add_filter( 'pewc_after_ajax_upload', 'demo_move_uploads_to_media_library', 10, 2 );

Was this article helpful?

Related Articles