Bad Support the Woocommerce
-
Hi Boss.. I have a solution for support Woo and some othet meta.. The problem lies in the imperfection of the function, which does not take into account some aspects DB replaceing..
This is happening because you are not updating additional meta fields and options. For example:
Code DB – Key meta_value
meta_value = "a:6:{s:5:"width";i:1000;s:6:"height";i:1000;s:4:"file";s:32:"2018/04/Miroos-cro-s.webp";s:5:"sizes";a:7:{s:12:"medium_large";a:5:{s:4:"file";s:32:"Miroos-cro-s-768x768.webp";s:5:"width";i:768;s:6:"height";i:768;s:9:"mime-type";s:10:"image/webp";s:8:"filesize";i:91668;}s:21:"woocommerce_thumbnail";a:6:{s:4:"file";s:32:"Miroos-cro-s-300x300.webp";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/webp";s:9:"uncropped";b:1;s:8:"filesize";i:21814;}s:18:"woocommerce_single";a:5:{s:4:"file";s:32:"Miroos-cro-s-600x600.webp";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/webp";s:8:"filesize";i:66512;}s:29:"woocommerce_gallery_thumbnail";a:5:{s:4:"file";s:32:"Miroos-cro-s-100x100.webp";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/webp";s:8:"filesize";i:4306;}s:12:"shop_catalog";a:5:{s:4:"file";s:31:"Miroos-cro-s-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";s:9:"uncropped";b:1;}s:11:"shop_single";a:4:{s:4:"file";s:31:"Miroos-cro-s-600x600.jpg";s:5:"width";i:600;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:14:"shop_thumbnail";a:4:{s:4:"file";s:31:"Miroos-cro-s-100x100.jpg";s:5:"width";i:100;s:6:"height";i:100;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}s:8:"filesize";i:149044;}"
ARRAY ()
meta_value = array(
'width' => 1000,
'height' => 1000,
'file' => '2018/04/Miroos-cro-s.webp',
'sizes' => array(
'medium_large' => array(
'file' => 'Miroos-cro-s-768x768.webp',
'width' => 768,
'height' => 768,
'mime-type' => 'image/webp',
'filesize' => 91668,
),
'woocommerce_thumbnail' => array(
'file' => 'Miroos-cro-s-300x300.webp',
'width' => 300,
'height' => 300,
'mime-type' => 'image/webp',
'uncropped' => true,
'filesize' => 21814,
),
'woocommerce_single' => array(
'file' => 'Miroos-cro-s-600x600.webp',
'width' => 600,
'height' => 600,
'mime-type' => 'image/webp',
'filesize' => 66512,
),
'woocommerce_gallery_thumbnail' => array(
'file' => 'Miroos-cro-s-100x100.webp',
'width' => 100,
'height' => 100,
'mime-type' => 'image/webp',
'filesize' => 4306,
),
'shop_catalog' => array(
'file' => 'Miroos-cro-s-300x300.jpg',
'width' => 300,
'height' => 300,
'mime-type' => 'image/jpeg',
'uncropped' => true,
),
'shop_single' => array(
'file' => 'Miroos-cro-s-600x600.jpg',
'width' => 600,
'height' => 600,
'mime-type' => 'image/jpeg',
),
'shop_thumbnail' => array(
'file' => 'Miroos-cro-s-100x100.jpg',
'width' => 100,
'height' => 100,
'mime-type' => 'image/jpeg',
),
),
'image_meta' => array(
'aperture' => '0',
'credit' => '',
'camera' => '',
'caption' => '',
'created_timestamp' => '0',
'copyright' => '',
'focal_length' => '0',
'iso' => '0',
'shutter_speed' => '0',
'title' => '',
'orientation' => '0',
'keywords' => array(),
),
'filesize' => 149044,
);As you can see, it is an array. Without my correction, it shows that your code is too simple and does not take into account WooCommerce meta support. Therefore, I advise you to listen to me rather than criticize my coding style. I’ve been coding since I was 15, and now I am 45. I’ve been working at Microsoft up to today, so I am trying to help you become even better.
Report on the Modification of the
change_db
Function Purpose of the ChangesThe primary goal of the changes was to correctly update all necessary entries in the WordPress database after converting images to the WebP format. This includes replacing old image URLs with new ones, especially for WooCommerce products. Issues with the Original Code
- Incomplete URL Updates: The original code did not update all necessary entries in the database. It only updated the
wp_posts
table, resulting in some image URLs remaining unchanged, particularly for WooCommerce products. - Lack of Updates in
wp_postmeta
andwp_options
Tables: The code did not account for updating image URLs in thewp_postmeta
andwp_options
tables, where image references are also stored.
Changes Made
- Added Updates in
wp_postmeta
andwp_options
Tables: Updates were added for thewp_postmeta
andwp_options
tables to ensure correct image URL references across the database. - Improved the
change_db
Function: The function was improved to perform URL replacements in three tables (wp_posts
,wp_postmeta
,wp_options
), ensuring a more comprehensive and correct database update.
Original Code
private function change_db($before_url, $after_url) { global $wpdb; /* Replace */ $wpdb->query( $wpdb->prepare( " UPDATE {$wpdb->prefix}posts SET post_content = replace(post_content, %s, %s) ", $before_url, $after_url ) ); }
Updated Code
private function change_db(string $before_url, string $after_url): void {
global $wpdb;
try {
// Ensure URLs are valid strings
if (empty($before_url) || empty($after_url)) {
throw new Exception('URLs must not be empty.');
}
// Update wp_posts table
$posts_query = $wpdb->prepare(
"UPDATE {$wpdb->prefix}posts SET post_content = REPLACE(post_content, %s, %s)",
$before_url,
$after_url
);
$posts_result = $wpdb->query($posts_query);
if ($posts_result === false) {
throw new Exception('Failed to update wp_posts table.');
}
// Update wp_postmeta table
$postmeta_query = $wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta SET meta_value = REPLACE(meta_value, %s, %s)",
$before_url,
$after_url
);
$postmeta_result = $wpdb->query($postmeta_query);
if ($postmeta_result === false) {
throw new Exception('Failed to update wp_postmeta table.');
}
// Update wp_options table
// Use a safe method to update options that may contain serialized data
$options = $wpdb->get_col("SELECT option_name FROM {$wpdb->prefix}options WHERE option_value LIKE '%{$before_url}%'");
foreach ($options as $option_name) {
$option_value = get_option($option_name);
if (is_array($option_value)) {
foreach ($option_value as $key => $value) {
if (is_string($value) && strpos($value, $before_url) !== false) {
$option_value[$key] = str_replace($before_url, $after_url, $value);
}
}
update_option($option_name, $option_value);
} elseif (is_string($option_value) && strpos($option_value, $before_url) !== false) {
$updated_value = str_replace($before_url, $after_url, $option_value);
update_option($option_name, $updated_value);
}
}
// Log success message
error_log("Successfully replaced URLs in wp_posts, wp_postmeta, and wp_options tables: $before_url to $after_url");
} catch (Exception $e) {
// Log the error message
error_log('Error changing database content: ' . $e->getMessage());
}
}Using the Updated Function
The
change_db
function should be called after converting an image to the WebP format and saving it on the server. The function takes two parameters: the URL of the image before and after the format change. This will ensure that all database records referencing this image are correctly updated.// Example of calling the change_db function
/* Replace */
$this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );This update ensures the correct replacement of all image URLs in the WordPress database, maintaining the stability and accuracy of your site’s functionality.
- Incomplete URL Updates: The original code did not update all necessary entries in the database. It only updated the
- You must be logged in to reply to this topic.