So im using IMGBB for my images as ive got way to many products, but no matter what i do Woocommerce will not let me use external links and just downloads them every time, So i made a function and it uploads to IMGBB but i cannot get it to change the Woocommerce product image locations
Any help would be Amazing ive been at this for days :S
Code:
// Function to upload an image to ImgBB and return the URL
function upload_image_to_imgbb($file_path) {
$api_key = ' ';
$image_data = file_get_contents($file_path);
$base64_image = base64_encode($image_data);
$url = 'https://api.imgbb.com/1/upload';
$data = array(
'key' => $api_key,
'image' => $base64_image
);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
if (isset($response['data']['url'])) {
error_log('ImgBB URL: ' . $response['data']['url']); // Debugging line
return $response['data']['url'];
} else {
error_log('ImgBB API response error: ' . $result);
return false;
}
}
// Function to handle the image upload process and update the URL
function handle_image_upload_to_imgbb($image_data) {
$file_path = $image_data['file'];
$imgbb_url = upload_image_to_imgbb($file_path);
if ($imgbb_url) {
// Ensure attachment is properly registered
$filetype = wp_check_filetype(basename($file_path), null);
$attachment = array(
'guid' => $image_data['url'],
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_path)),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $file_path);
if (is_wp_error($attachment_id)) {
error_log('Failed to insert attachment: ' . $attachment_id->get_error_message());
} else {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attachment_id, $file_path);
wp_update_attachment_metadata($attachment_id, $attach_data);
update_post_meta($attachment_id, '_imgbb_url', $imgbb_url);
error_log('Meta updated for attachment ID: ' . $attachment_id . ' with URL: ' . $imgbb_url); // Debugging line
// Update the image_data array to return the ImgBB URL
$image_data['url'] = $imgbb_url;
}
} else {
error_log('ImgBB upload failed for file: ' . $file_path);
}
return $image_data;
}
add_filter('wp_handle_upload', 'handle_image_upload_to_imgbb');
// Conditional URL replacement for local development environment
if ($_SERVER['REMOTE_ADDR'] === '127.0.0.1') {
// Replace src paths
add_filter('wp_get_attachment_url', function ($url) {
error_log('wp_get_attachment_url - Original URL: ' . $url); // Debugging line
$attachment_id = attachment_url_to_postid($url);
if ($attachment_id) {
error_log('wp_get_attachment_url - Attachment ID: ' . $attachment_id); // Debugging line
$imgbb_url = get_post_meta($attachment_id, '_imgbb_url', true);
if ($imgbb_url) {
error_log('wp_get_attachment_url - Replaced URL: ' . $imgbb_url); // Debugging line
return $imgbb_url;
} else {
error_log('wp_get_attachment_url - ImgBB URL not found for attachment ID: ' . $attachment_id); // Debugging line
}
} else {
error_log('wp_get_attachment_url - Attachment ID not found for URL: ' . $url); // Debugging line
}
return $url;
});
// Replace srcset paths
add_filter('wp_calculate_image_srcset', function ($sources) {
foreach ($sources as &$source) {
error_log('wp_calculate_image_srcset - Original Srcset URL: ' . $source['url']); // Debugging line
$attachment_id = attachment_url_to_postid($source['url']);
if ($attachment_id) {
error_log('wp_calculate_image_srcset - Attachment ID: ' . $attachment_id); // Debugging line
$imgbb_url = get_post_meta($attachment_id, '_imgbb_url', true);
if ($imgbb_url) {
error_log('wp_calculate_image_srcset - Replaced Srcset URL: ' . $imgbb_url); // Debugging line
$source['url'] = $imgbb_url;
} else {
error_log('wp_calculate_image_srcset - ImgBB URL not found for srcset attachment ID: ' . $attachment_id); // Debugging line
}
} else {
error_log('wp_calculate_image_srcset - Srcset Attachment ID not found for URL: ' . $source['url']); // Debugging line
}
}
return $sources;
});
}
// Function to replace image URLs in post content
function replace_image_urls_in_content($content) {
// Regular expression to match all image URLs
$pattern = '/<img[^>]+src="([^">]+)"/i';
preg_match_all($pattern, $content, $matches);
$image_urls = $matches[1];
foreach ($image_urls as $url) {
error_log('replace_image_urls_in_content - Original URL: ' . $url); // Debugging line
$attachment_id = attachment_url_to_postid($url);
if ($attachment_id) {
$imgbb_url = get_post_meta($attachment_id, '_imgbb_url', true);
if ($imgbb_url) {
error_log('replace_image_urls_in_content - Replacing ' . $url . ' with ' . $imgbb_url); // Debugging line
$content = str_replace($url, $imgbb_url, $content);
} else {
error_log('replace_image_urls_in_content - ImgBB URL not found for attachment ID: ' . $attachment_id); // Debugging line
}
} else {
error_log('replace_image_urls_in_content - Attachment ID not found for URL: ' . $url); // Debugging line
}
}
return $content;
}
add_filter('the_content', 'replace_image_urls_in_content');
Any help would be Amazing ive been at this for days :S