public
Created

CHANGE - Regenerete Thumbnail Wordpress

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?php
 
// Process a single image ID (this is an AJAX handler)
function ajax_process_image() {
@error_reporting( 0 ); // Don't break the JSON result
 
header( 'Content-type: application/json' );
 
$id = (int) $_REQUEST['id'];
$image = get_post( $id );
 
if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) ) {
die( json_encode( array( 'error' => sprintf( __( 'Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails' ), esc_html( $_REQUEST['id'] ) ) ) ) );
return;
}
 
if ( ! current_user_can( $this->capability ) ) {
$this->die_json_error_msg( $image->ID, __( "Your user account doesn't have permission to resize images", 'regenerate-thumbnails' ) );
return;
}
 
$fullsizepath = get_attached_file( $image->ID );
 
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) {
$this->die_json_error_msg( $image->ID, sprintf( __( 'The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails' ), '<code>' . esc_html( $fullsizepath ) . '</code>' ) );
return;
}
@set_time_limit( 900 ); // 5 minutes per image should be PLENTY
// ############
// DELETE ALL THUMBNAIL, TO REALLY REGENERETE =D
// By: Pedro Elsner
// ############
global $_wp_additional_image_sizes;
$imagePath = substr($fullsizepath, 0, strlen($fullsizepath) - 4);
$imageFormat = substr($fullsizepath, -4);
foreach($_wp_additional_image_sizes as $key => $value) {
$thumb = $imagePath . '-' . $value['width'] . 'x' . $value['height'] . $imageFormat;
unlink($thumb);
}
// ############
$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
 
if ( is_wp_error( $metadata ) ) {
$this->die_json_error_msg( $image->ID, $metadata->get_error_message() );
return;
}
if ( empty( $metadata ) ) {
$this->die_json_error_msg( $image->ID, __( 'Unknown failure reason.', 'regenerate-thumbnails' ) );
return;
}
wp_update_attachment_metadata( $image->ID, $metadata );
 
die( json_encode( array( 'success' => sprintf( __( '&quot;%1$s&quot; (ID %2$s) was successfully resized in %3$s seconds.', 'regenerate-thumbnails' ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ) ) ) );
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.