Skip to content

Instantly share code, notes, and snippets.

View BhargavBhandari90's full-sized avatar
🏠
Working from home

Bhargav(Bunty) BhargavBhandari90

🏠
Working from home
View GitHub Profile
<?php
function fp_scan_all_nonoptimized_images()
{
$upload_dir = wp_upload_dir();
$uploads_path = $upload_dir['basedir'];
$uploads_url = $upload_dir['baseurl'];
$optimized_path = WP_CONTENT_DIR . '/flying-press-images';
$batch = [];
<?php
// Get non-optimised image list in batch with generator
function fp_scan_all_images()
{
$upload_dir = wp_upload_dir();
$uploads_path = $upload_dir['basedir'];
$uploads_url = $upload_dir['baseurl'];
$optimized_path = WP_CONTENT_DIR . '/flying-press-images';
$batch = [];
<?php
// Get difference of two folders and get non-optimised images
function fp_scan_all_images()
{
$upload_dir = wp_upload_dir();
$uploads_path = $upload_dir['basedir'];
$uploads_url = $upload_dir['baseurl'];
$optimized_path = WP_CONTENT_DIR . '/flying-press-images';
// Step 1: Build lookup for optimized files
<?php
function fp_scan_images_generator()
{
try {
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];
// Recursive scan with FileSystemIterator
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
<?php
// Log files in oneshot - NO generator
function fp_scan_images_oneshot($only_non_optimized = false)
{
$base_directory = wp_upload_dir();
$directory = $base_directory['basedir'];
$optimized_directory = WP_CONTENT_DIR . '/flying-press-images';
// Recursive scan with FileSystemIterator
$iterator = new RecursiveIteratorIterator(
@BhargavBhandari90
BhargavBhandari90 / fp-scan-images-batch-v4.php
Created September 17, 2025 18:09
Log image links to error log file
<?php
/**
* Log scanned image links to debug.log.
*
* Make sure to enable WP_DEBUG_LOG from wp-config.php to use this command.
*/
function process_image_batch_new($batch_size = 500, $reset = false)
{
$upload_dir = wp_get_upload_dir();
$batch = [];
@BhargavBhandari90
BhargavBhandari90 / fp-scan-images-batch-v3.php
Last active September 17, 2025 15:28
Scan images in bacth and store to json file.
<?php
// Scan images in batch
function fp_image_scan_batch($batch_size = 500, $reset = false)
{
$upload_dir = wp_get_upload_dir();
$batch = [];
$log_file = trailingslashit(WP_CONTENT_DIR) . 'fp-image-scan.log';
// Reset log file on first batch
@BhargavBhandari90
BhargavBhandari90 / wp-scan-images-batch-v2.php
Created September 17, 2025 09:17
Batch with Memory Efficiency
<?php
// Batch Process
function process_image_batch($batch_size = 100, $reset = false)
{
$directory = wp_get_upload_dir()['basedir'];
$json_file = trailingslashit(WP_CONTENT_DIR) . 'image.json';
// First batch → reset file
if ($reset || !file_exists($json_file)) {
@BhargavBhandari90
BhargavBhandari90 / wp-scan-images-batch.php
Created September 15, 2025 10:46
Scan Images in Bacthes using filesystem in WordPress
<?php
// Scan images from uploads folder
function fp_scan_images($offset = 0, $batch_size = 500)
{
try {
$images = [];
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];
// Recursive scan with FileSystemIterator
@BhargavBhandari90
BhargavBhandari90 / wp-scan-images.php
Last active September 16, 2025 18:15
Iterate Images form uploads folder
<?php
// Scan images from uoloads folder including sub-folders.
function fp_scan_images() {
try {
$images = [];
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];
// Recursive scan with FileSystemIterator