Skip to content

Instantly share code, notes, and snippets.

@REPTILEHAUS
Created October 19, 2016 12:37
Show Gist options
  • Save REPTILEHAUS/b2fca5f9e4d208000968ae1862b39c7e to your computer and use it in GitHub Desktop.
Save REPTILEHAUS/b2fca5f9e4d208000968ae1862b39c7e to your computer and use it in GitHub Desktop.
WP Nightly Image OptimizatioN
# This cronjob is set to run at 130am every single night
30 1 * * * /usr/bin/php -q /var/www/yourwebsitesroot/wp-content/themes/YOURTHEME/skxvnvhthz2k4w.php
# This is a commented out cronjob to run every single minute - do not use this except to test that your server is executing the cron
# */1 * * * * /usr/bin/php -q /var/www/yourwebsitesroot/wp-content/themes/YOURTHEME/skxvnvhthz2k4w.php
<?php
global $wpdb;
require('/var/www/yourwebsitesroot/wp-blog-header.php');
// require ( ABSPATH . 'wp-admin/includes/image.php' );
$images = $wpdb->get_results(
"SELECT ID
FROM $wpdb->posts
WHERE post_type = 'attachment'
AND post_mime_type
LIKE 'image/%'
AND DATE(`post_date`) > DATE_SUB(CURDATE(),
INTERVAL 2 DAY)"
);
foreach ( $images as $image ) {
$fullsizepath = get_attached_file( $image->ID );
wp_update_attachment_metadata( $image->ID, wp_generate_attachment_metadata( $image->ID, $fullsizepath ) );
}
?>
# switch to the root account if you need to
sudo -s
# Then create the script using either of the following
vim skxvnvhthz2k4w.php
# OR
nano skxvnvhthz2k4w.php
# OR
touch skxvnvhthz2k4w.php
# Now change the files permissions so that only the root user may read, write and execute it.
chmod u+rwx skxvnvhthz2k4w.php
php /var/www/yourwebsitesroot/wp-content/themes/YOURTHEME/skxvnvhthz2k4w.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment