WP Fitter

Guiding WordPress journey, from setup to success

Implementing Smart Retention Policies with Advanced Media Offloader

Smart Retention Policies

As the developer of Advanced Media Offloader, I’m constantly working to provide more flexibility in how WordPress site owners manage their media storage. Today, I want to share a powerful technique that gives you complete control over which files get offloaded to cloud storage and when.

Why Custom Retention Policies Matter

Every WordPress site has unique media management needs. Some users want to delay offloading for a few hours, others need to keep certain file types local permanently, and some require different rules based on file size or who uploaded them. Traditional offloading plugins follow a one-size-fits-all approach, but real-world scenarios demand more flexibility:

  • Time-based policies: Keep files local for a buffer period before offloading
  • Size-based filtering: Only offload large files while keeping thumbnails local for performance
  • File type exclusions: Keep SVGs and critical assets local while offloading heavy media
  • User-based rules: Apply different policies for admin uploads vs contributor content
  • Conditional logic: Combine multiple factors to create sophisticated workflows

These policies help you optimize costs, improve site performance, and maintain the perfect balance between local and cloud storage.

The Solution: Custom Retention Policies

Advanced Media Offloader provides a powerful filter hook called advmo_should_offload_attachment that allows you to implement custom logic for determining which files should be offloaded. Here’s how you can create a time-based retention policy.

Setting Up a Delay Period

The following code snippet prevents files from being offloaded if they were uploaded less than 5 minutes ago:

/**
 * 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) < (5 * MINUTE_IN_SECONDS)) {
            return false; // Skip offloading if younger than 5 minutes
        }
        
        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);

Customizing the Retention Period

You can easily adjust the retention period to suit your needs:

  • For a 1-hour delay: (60 * MINUTE_IN_SECONDS)
  • For a 24-hour delay: (24 * HOUR_IN_SECONDS)
  • For a 7-day delay: (7 * DAY_IN_SECONDS)

Automating the Offload Process

Once you’ve implemented the retention policy, you’ll want to set up an automated process to offload files after the delay period. This is where WP-CLI and cron jobs come into play.

Setting Up a Cron Job

Add the following command to your server’s crontab to automatically offload eligible files every 30 minutes:

*/30 * * * * cd /path/to/public_html && wp advmo offload

This ensures that files are regularly checked and offloaded once they exceed your defined retention period.

Advanced Use Cases

The advmo_should_offload_attachment filter opens up many possibilities beyond simple time-based retention:

1. Size-Based Policies

Only offload files larger than a certain size, keeping smaller files local for faster access:

function wpfitter_offload_large_files_only($should_offload, $attachment_id, $metadata = null)
{
    $file_path = get_attached_file($attachment_id);
    if ($file_path && file_exists($file_path)) {
        $size_mb = filesize($file_path) / (1024 * 1024);
        if ($size_mb < 1) { // Keep files under 1MB local
            return false;
        }
    }
    return $should_offload;
}
add_filter('advmo_should_offload_attachment', 'wpfitter_offload_large_files_only', 10, 3);

2. File Type Policies

Keep certain file types local while offloading others:

function wpfitter_selective_file_type_offload($should_offload, $attachment_id, $metadata = null)
{
    $mime_type = get_post_mime_type($attachment_id);
    $keep_local = ['image/svg+xml', 'application/pdf'];
    
    if (in_array($mime_type, $keep_local)) {
        return false;
    }
    return $should_offload;
}
add_filter('advmo_should_offload_attachment', 'wpfitter_selective_file_type_offload', 10, 3);

3. User-Based Policies

Apply different retention rules based on who uploaded the file:

function wpfitter_user_based_retention($should_offload, $attachment_id, $metadata = null)
{
    $post = get_post($attachment_id);
    if ($post && $post->post_author == 1) { // Admin uploads
        // Apply longer retention for admin uploads
        $created_gmt = get_post_time('U', true, $post);
        $now_gmt = current_time('timestamp', true);
        if (($now_gmt - $created_gmt) < (24 * HOUR_IN_SECONDS)) {
            return false;
        }
    }
    return $should_offload;
}
add_filter('advmo_should_offload_attachment', 'wpfitter_user_based_retention', 10, 3);

Best Practices

When implementing retention policies, keep these tips in mind:

  1. Use try-catch blocks to prevent errors from interrupting the upload process
  2. Test thoroughly in a staging environment before deploying to production
  3. Monitor your cron jobs to ensure they’re running as expected
  4. Consider your traffic patterns when setting delay periods and cron intervals
  5. Document your custom code for future reference and maintenance

Conclusion

The flexibility of Advanced Media Offloader‘s filter system allows you to create sophisticated retention policies that match your specific workflow needs. Whether you need a simple time delay, complex conditional logic, or a combination of factors, the advmo_should_offload_attachment filter provides the foundation for building exactly what you need.

By implementing smart retention policies, you can optimize your media management strategy, reduce costs, and maintain the perfect balance between local performance and cloud storage efficiency.

Have you implemented a creative retention policy with Advanced Media Offloader? I’d love to hear about your use case and how you’re using the plugin to solve your media storage challenges!

Leave a Reply