-
-
Save danpoynor/3b3566745abe59cd06160840cdebca01 to your computer and use it in GitHub Desktop.
Insanely fast multi-core Node.js script for generating (or regenerating) custom WordPress image sizes (e.g. thumbnails) for very large media libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your server must have wp-cli installed (http://wp-cli.org/) | |
var async = require("async"); | |
var exec = require("child_process").exec; | |
var cores = 32; // Adjust the number of cores to match your environment | |
var total = 0; | |
var finished = 0; | |
var q = async.queue(function(task, callback) { | |
exec(task.command, function(error, stdout, stderr) { | |
finished++; | |
process.stdout.write("Processing " + total + " images using " + cores + " cores. Progress: " + (100 * (finished / total)).toFixed(2) + "%\r"); | |
callback(); | |
}); | |
}, cores); | |
q.drain = function() { | |
console.log("Finished processing " + total + " images using " + cores + " cores. "); | |
process.exit(); | |
}; | |
// Get all media post IDs | |
exec("wp db query \"SELECT id FROM wp_posts WHERE post_type = 'attachment'\"", {maxBuffer: 1000 * 1024}, function(error, stdout, stderr) { | |
var id = stdout.split(/\n/); | |
for (var i = 1; i < id.length; i++) { | |
q.push({command: "wp media regenerate " + id[i]}); | |
total++; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment