Last 24h
-
Hello,
Is it possible to add an option to refresh only images from the last 24 hours?
Since my website runs entirely on webp (without an external plugin) and I always have the jpg files automatically deleted, I always have to refresh the images, and that always takes forever with several hundred thousand images, as the plugin goes through all of them.
-
Hi @saguya
Thanks for your suggestion regarding refreshing only images from the last 24 hours. At this time, the plugin does not support this functionality and there isn’t a built-in option to selectively refresh images based on date. Since the plugin is not currently in active development, implementing such a feature would likely require custom code or a developer to create a custom solution. If you need targeted image refreshing, you might want to explore writing a custom script or hiring a developer familiar with WordPress media handling.
For some additional context, @saguya : our dearest Alex, original developer of this plugin, lost his battle with Leukemia in 2019, and has since left our world. We, his friends and colleagues, have committed to provide support for his open source plugins, but we have fairly limited resources to do so. Hence @drawmyface’s reply above. Thanks for your understanding, and hoping you will still get to enjoy Alex’s work for what they provide you with.
-
This reply was modified 1 month, 2 weeks ago by
Stephane Daury (stephdau).
-
This reply was modified 1 month, 2 weeks ago by
Stephane Daury (stephdau).
-
This reply was modified 1 month, 2 weeks ago by
Stephane Daury (stephdau).
Hello,
For the late reply, and my condolences for the loss of a loved one.
A few days ago, I wrote a PHP script that does exactly what I want, and it runs daily via cron.
I wrote the script specifically for my system, but I think it can be used for general purposes as well.<?php
/**
* Auto-WebP Daily Converter (MariaDB + WP Integration)
* ----------------------------------------------------
* - Converts new JPG/JPEG images from the last 24 hours to WebP
* - Deletes originals after successful conversion
* - Automatically updates the WP database (MariaDB)
* - Runs "wp media regenerate --yes --skip-ssl"
* - Sends a status email with results
* - Runs automatically daily via cron job
*/
$upload_dir = 'wp-content/uploads/';
$wp_path = 'complete WP Path here';
$email_to = 'your mail'; // ✉️ Admin-Mail here
// ========== LOCK FILE ==========
$lock_file = sys_get_temp_dir() . '/auto-webp-daily.lock';
$lock_fp = @fopen($lock_file, 'c');
if (!$lock_fp || !flock($lock_fp, LOCK_EX | LOCK_NB)) {
exit("⚠️ Script is already running.\n");
}
register_shutdown_function(function() use ($lock_fp, $lock_file) {
flock($lock_fp, LOCK_UN);
fclose($lock_fp);
@unlink($lock_file);
});
// ========== WP-CONFIG READ ==========
$wp_config = file_get_contents($wp_path . '/wp-config.php');
if (!$wp_config) {
exit("❌ Could not read wp-config.php.\n");
}
preg_match("/define\(\s*'DB_NAME'\s*,\s*'([^']+)'\s*\)/", $wp_config, $db_name);
preg_match("/define\(\s*'DB_USER'\s*,\s*'([^']+)'\s*\)/", $wp_config, $db_user);
preg_match("/define\(\s*'DB_PASSWORD'\s*,\s*'([^']+)'\s*\)/", $wp_config, $db_pass);
preg_match("/define\(\s*'DB_HOST'\s*,\s*'([^']+)'\s*\)/", $wp_config, $db_host);
if (!$db_name || !$db_user || !$db_pass || !$db_host) {
exit("❌ Could not read DB credentials from wp-config.php.\n");
}
$db_name = $db_name[1];
$db_user = $db_user[1];
$db_pass = $db_pass[1];
$db_host = $db_host[1];
// ========== CONNECTION WITH MARIADB ==========
$db = mysqli_init();
mysqli_real_connect(
$db,
$db_host,
$db_user,
$db_pass,
$db_name,
null,
null,
MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT // skip-ssl
);
if (mysqli_connect_errno()) {
exit("❌ MariaDB connection failed: " . mysqli_connect_error() . "\n");
}
// ========== START ==========
$now = date('Y-m-d H:i:s');
$since = time() - 86400; // letzte 24h
$converted = 0;
$deleted = 0;
$output_html = "<h2>Auto-WebP Daily Result</h2><ul>";
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($upload_dir));
foreach ($rii as $file) {
if ($file->isDir()) continue;
$path = $file->getPathname();
if (!preg_match('/\.(jpg|jpeg)$/i', $path)) continue;
if (filemtime($path) < $since) continue;
$webp_path = preg_replace('/\.(jpg|jpeg)$/i', '.webp', $path);
try {
$img = new Imagick($path);
$img->setImageFormat('webp');
$img->setOption('webp:method', '6');
$img->setImageCompressionQuality(60);
$img->writeImage($webp_path);
$img->clear(); $img->destroy();
if (file_exists($webp_path) && filesize($webp_path) > 0) {
unlink($path);
$converted++;
$output_html .= "<li>✅ {$path} → {$webp_path}</li>";
} else {
$output_html .= "<li>⚠️ Error in {$path}</li>";
}
} catch (Exception $e) {
$output_html .= "<li>❌ Imagick error at{$path}: {$e->getMessage()}</li>";
}
}
// ========== DB UPDATE ONLY FOR LAST 24H ==========
$queries = [
"UPDATEwp_postsSET guid = REPLACE(guid, '.jpg', '.webp') WHERE guid LIKE '%.jpg' AND post_date >= NOW() - INTERVAL 1 DAY;",
"UPDATEwp_postsSET guid = REPLACE(guid, '.jpeg', '.webp') WHERE guid LIKE '%.jpeg' AND post_date >= NOW() - INTERVAL 1 DAY;",
"UPDATEwp_postmetaSET meta_value = REPLACE(meta_value, '.jpg', '.webp') WHERE meta_value LIKE '%.jpg' AND post_id IN (SELECT ID FROMwp_postsWHERE post_date >= NOW() - INTERVAL 1 DAY);",
"UPDATEwp_postmetaSET meta_value = REPLACE(meta_value, '.jpeg', '.webp') WHERE meta_value LIKE '%.jpeg' AND post_id IN (SELECT ID FROMwp_postsWHERE post_date >= NOW() - INTERVAL 1 DAY);",
"UPDATEwp_postsSET post_mime_type='image/webp' WHERE post_mime_type IN ('image/jpeg','image/jpg') AND post_date >= NOW() - INTERVAL 1 DAY;"
];
foreach ($queries as $sql) {
mysqli_query($db, $sql);
}
mysqli_close($db);
// ========== WP MEDIA REGENERATE ==========
chdir($wp_path);
exec("wp media regenerate --yes --skip-ssl 2>&1", $wp_output);
$output_html .= "</ul><pre>" . htmlspecialchars(implode("\n", $wp_output)) . "</pre>";
$output_html .= "<p><strong>Time:</strong> {$now}</p>";
$output_html .= "<p><strong>Converted:</strong> {$converted} Datei(en)</p>";
$output_html .= "<p><strong>Status:</strong> ✅ Fertig</p>";
// ========== MAIL SEND ==========
$subject = "✅ Auto-WebP completed – {$now}";
$headers = "Content-Type: text/html; charset=UTF-8\r\n";
mail($email_to, $subject, $output_html, $headers);
echo "✅ Auto-WebP completed um {$now} ({$converted} Converted)\n";
?>Thanks for sharing that, @saguya
I am sure that other users who want similar functionality will appreciate it. Our team will consider implementing such functionality in the future, too.
-
This reply was modified 1 month, 2 weeks ago by
You must be logged in to reply to this topic.