How to set Offload Delay?
-
Hi, I like how AMO plugin simplified the process of offloading.
I am trying to find a way how to delay every uploaded files to be offload.
eg. delay it after 30 minutes.
Thank you.
-
Hi @yajwp
Thanks for the feedback!
You can find your answer by reading this guide: https://wpfitter.com/blog/implementing-smart-retention-policies-with-advanced-media-offloader/Hi @masoudin , Thanks for the reply.
I tried code insertion but the AMO flow offload is still real time or still offloads fast.
Settings “Full Cloud Migration”.
Do I need another way around like code inserting within the plugin it self?
Thank you so much for you reply.@yajwp
Could you please share your code here with us? For how long did you delayed the offloading?
Do you have a specific purpose to delay the offload?Hi @masoudin ,
I inserted the code to the Child functions and I also tried it on code snippet. I set it to 30 minutes for testing purposes, if it goes well this will be permanently set to(60 * MINUTE_IN_SECONDS)if (is_numeric($created_gmt) && ($now_gmt - (int) $created_gmt) < (30 * MINUTE_IN_SECONDS)) {
return false; // Skip offloading if younger than 5 minutes
}Regarding purpose. I am currently installing my mu and what it does in an audio upload process, it replaces or re-write the metadata first before it offloads (getid3 function re-write).
On top of my mu, I also tried to combine your sample retention script on top of my mu like conditional logic.
Plugin setting: Full Cloud Migration. Even if I try to set “Retain Local Files” it offloads too fast.Thank you for your reply.
Hi @yajwp
After uploading a file to your Media Library, the file gets immediately offloaded to the cloud storage. But you can delay the offloading process by using the following code and adding it to your theme / child-theme functions.php:/**
* Prevent offloading of newly uploaded attachments (younger than 5 minutes).
* @param bool $should_offload Current decision.
* @param int $attachment_id Attachment ID.
* @param array|null $metadata Attachment metadata when available.
* @return bool False to skip offloading.
*/
function wpfitter_prevent_recent_offload($should_offload, $attachment_id, $metadata = null)
{
try {
$post = get_post($attachment_id);
if (!$post) {
return $should_offload;
}
// Compare upload time (GMT) against now (GMT)
$created_gmt = get_post_time('U', true, $post);
$now_gmt = current_time('timestamp', true);
if (is_numeric($created_gmt) && ($now_gmt - (int) $created_gmt) < (30 * MINUTE_IN_SECONDS)) {
return false;
}
return $should_offload;
} catch (\Throwable $e) {
// Fail open to avoid interrupting uploads/offload flow
return $should_offload;
}
}
add_filter('advmo_should_offload_attachment', 'wpfitter_prevent_recent_offload', 10, 3);Note:
Retention policies like “Retain Local Files” or “Full Cloud Policy” doesn’t delay the offloading process. They are rules to define if a file should be deleted completely (Full Cloud Migration) from the local server after a successful offload or it should keep them on the local server and do not delete anything (Retain Local Files).Ok I will try it again inserting it to the child theme and follow the steps on your website blog.
Thank you so much @masoudin for the support.@masoudin Awesome update “Auto-Offload Media = Automatically send new uploads to cloud storage. Note: Existing offloaded files will always load from the cloud, even if this is disabled.“
I can use some of my functions when temporarily stopped and use the cron.
Thanks for the update.Hi @yajwp
Thanks for using the plugin and the feedback
You must be logged in to reply to this topic.