Last active
December 14, 2022 08:29
-
-
Save amrikarisma/08b503291a726b8a039d00199d739386 to your computer and use it in GitHub Desktop.
WordPress generate new size for old posts
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
<?php | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
// Put the function in a class to make it more extendable | |
class KRS_regen_media | |
{ | |
public function krs_regenerate($imageId) | |
{ | |
$imagePath = wp_get_original_image_path($imageId); | |
if ($imagePath && file_exists($imagePath)) { | |
$attach_data = wp_generate_attachment_metadata($imageId, $imagePath); | |
return wp_update_attachment_metadata($imageId, $attach_data); | |
} else { | |
return "File not exist"; | |
} | |
} | |
} | |
// Add a load function | |
function krs_regen_load() | |
{ | |
if (isset($_GET['thumb-regenerate'])) { | |
$regenerateThumb = sanitize_text($_GET['thumb-regenerate']); | |
if ($regenerateThumb == '1') { | |
$uploads_directory = wp_upload_dir(); | |
$all_posts = get_posts(array( | |
'fields' => 'ids', | |
'numberposts' => -1 | |
)); | |
echo '<pre>'; | |
$generateImage = []; | |
$generateImageFail = []; | |
foreach ($all_posts as $postId) { | |
$imageId = get_post_thumbnail_id($postId); | |
if ($imageId != 0) { | |
$am = wp_get_attachment_image_url($imageId, 'size-card'); | |
if (!preg_match('/(-720x405)/', $am)) { | |
// Instantiate the class | |
$krs_regen_media = new KRS_regen_media(); | |
// You can get the image Id from the url of the media screen | |
$regenerate = $krs_regen_media->krs_regenerate($imageId); | |
if ($regenerate != 'File not exist' && $regenerate !== false) { | |
$generateImage[] = [ | |
'id' => $imageId, | |
'image' => $am, | |
]; | |
} else if ($regenerate == 'File not exist') { | |
$generateImageFail[] = [ | |
'id' => $imageId, | |
'image' => 'File not exist', | |
]; | |
} | |
// thumbnail_cleaner_rmdir($uploads_directory['basedir']); | |
} else { | |
$generateImage[] = [ | |
'id' => $imageId, | |
'image' => $am, | |
]; | |
} | |
} | |
} | |
$result = [ | |
'post_count' => count($all_posts), | |
'total_success' => count($generateImage), | |
'total_failed' => count($generateImageFail), | |
'generate' => array_merge($generateImage, $generateImageFail), | |
]; | |
print_r(json_encode($result)); | |
} | |
die(); | |
} | |
} | |
// Load after WordPress has finished loading | |
add_action('init', 'krs_regen_load'); | |
function thumbnail_cleaner_rmdir($directory) | |
{ | |
$objects = (array)scandir($directory); | |
foreach ($objects as $object) { | |
if ($object !== "." && $object !== "..") { | |
if (filetype($directory . "/" . $object) === "dir") { | |
thumbnail_cleaner_rmdir($directory . "/" . $object); | |
} else { | |
// Remove unused file thumbnail | |
if (preg_match('/(.webp)/', $object)) { | |
unlink($directory . "/" . $object); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment