Write a WordPress image localization function. This function will automatically download external images to the media library when publishing or updating articles.
<? php//functions.php
/**
Download external images to your local media library
*/
Function download_external_images ($post_id) {
//Get the content of the article
$Post = get_post ($post_id);
$Content = $post- > post_content;
If the content is empty, return
If (empty ($content)) {
Return;
}
Match all image tags
$Pattern = '/< img [^ >] + src =[\'"]([^\'"]+)[\'"][^>]*>/ i';
preg_match_all ($pattern, $content, $matches);
If (empty ($matches [1])) {
Return;
}
//Get the site URL
$site_url = get_site_url ();
$updated = false;
Foreach ($matches [1] as $image_url) {
//Skip the pictures on this site
If (strpos ($image_url, $site_url)! == false) {
Continue;
}
//download the picture
$Tmp = download_url ($image_url);
If (is_wp_error ($tmp)) {
Continue;
}
//Get file information
$file_array = array (
'Name '= > basename ($image_url),
'tmp_name '= > $tmp
);
//Check the file type
$file_type = wp_check_filetype ($file_array ['name']);
If (! $file_type ['type']) {
Unlink ($tmp);
Continue;
}
//Add the image to the media library
$attachment_id = media_handle_sideload ($file_array, $post_id);
If (is_wp_error ($attachment_id)) {
Unlink ($tmp);
Continue;
}
//Get a new image URL
$new_url = wp_get_attachment_url ($attachment_id);
//Replace the image URL in the content
$Content = str_replace ($image_url, $new_url, $content);
$updated = true;
}
//If there is an update, save the article content
IF ($updated) {
wp_update_post (array (
'ID '= > $post_id,
'post_content '= > $content
));
}
}
//Add a hook to trigger when saving the article
add_action ('save_post', 'download_external_images', 10,1);
/**
* Add image failed to download error handling
*/
Function handle_download_error ($error_msg, $error_code, $error_data, $file) {
error_log ("Image download failed : " . $ error_msg);
Return new WP_Error ($error_code, $error_msg, $error_data);
}
add_filter ('wp_handle_upload_error', 'handle_download_error', 10,4);
Main function description:
Auto-trigger: automatically runs when an article is saved or updated
Image detection: Use regex (regular expression) to match all images in the content of the article
Localization:
Skip the pictures on this site
Download external images
Add to Media Library
Update the image URL in the article content
Error handling:
Check file type
Handling failed to download
Log errors
How to use:
Add the code to the theme's functions.php file
When you publish or edit an article, the function runs automatically
External images are automatically downloaded and replaced with local URLs.
Performance optimization:
Only process external pictures.
Working with files with WordPress build-in function
Update article content only when there are changes
Add error handling to avoid interruptions
Notes:
Make sure WordPress has write permissions
Check that the media library has sufficient storage space
You may need to adjust the PHP execution time limit
It is recommended to test in the testing environment first