Batch import the external link pictures in WordPress articles to local

WordPress thirty-one 5.2K Reading mode

It is reported that Sina Blog has officially announced that the "photo album" function is offline, and the deadline is 24:00 on July 31, 2019. Another free lunch is gone.

How to download the pictures placed in Sina album to local and replace the original link has been put in front of many users who use Sina free graphic bed, including me of course. After searching and testing in many ways, I will share my final solution to everyone.

Many WordPress plug-ins or codes can automatically download the external link pictures to the local in editing articles. Finally, I chose a plug-in called Easy Copy Paste.

 

Extract code : khc5

 

You can also directly add the following code to the current topic function template functions.php:

 function ecp_save_post($post_id, $post) { global $wpdb; if($post->post_status == 'publish') { $p   = '/<img.*[\s]src=[\"|\'](.*)[\"|\'].*>/iU'; $num = preg_match_all($p, $post->post_content, $matches); if ($num) { $wp_upload_dir = wp_upload_dir(); set_time_limit(0); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS,20); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $ecp_options = $_SERVER['HTTP_HOST']; foreach ($matches[1] as $src) { if (isset($src) && strpos($src, $ecp_options) === false) { $file_info = wp_check_filetype(basename($src), null); if ($file_info['ext'] == false) { date_default_timezone_set('PRC'); $file_name = date('YmdHis-').dechex(mt_rand(100000, 999999)).'. tmp'; } else { $file_name = dechex(mt_rand(100000, 999999)) . '-' .  basename($src); } curl_setopt($ch, CURLOPT_URL, $src); $file_path = $wp_upload_dir['path'] . '/' . $ file_name; $img = fopen($file_path, 'wb'); curl_setopt($ch, CURLOPT_FILE, $img); $img_data  = curl_exec($ch); fclose($img); if (file_exists($file_path) && filesize($file_path) > 0) { $t   = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $arr = explode('/', $t); if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp') { $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'tmp'); } elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp') { $file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'webp'); } $post->post_content  = str_replace($src, $wp_upload_dir['url'] . '/' . basename($file_path), $post->post_content); $attachment = ecp_get_attachment_post(basename($file_path), $wp_upload_dir['url'] . '/' .  basename($file_path)); $attach_id = wp_insert_attachment($attachment, ltrim($wp_upload_dir['subdir'] . '/' . basename($file_path), '/'), 0); $attach_data = wp_generate_attachment_metadata($attach_id, $file_path); $ss = wp_update_attachment_metadata($attach_id, $attach_data); } } } curl_close($ch); $wpdb->update( $wpdb->posts, array('post_content' => $post->post_content), array('ID' => $post->ID)); } } } function ecp_handle_ext($file, $type, $file_dir, $file_name, $ext) { switch ($ext) { case 'tmp': if (rename($file, str_replace('tmp', $type, $file))) { if ('webp' == $type) { return ecp_image_convert('webp', 'jpeg', $file_dir . '/' . str_replace('tmp', $type, $file_name)); } return $file_dir . '/' .  str_replace('tmp', $type, $file_name); } case 'webp': if ('webp' == $type) { return ecp_image_convert('webp', 'jpeg', $file); } else { if (rename($file, str_replace('webp', $type, $file))) { return $file_dir . '/' .  str_replace('webp', $type, $file_name); } } default: return $file; } } function ecp_image_convert($from='webp', $to='jpeg', $image) { $im = imagecreatefromwebp($image); if (imagejpeg($im, str_replace('webp', 'jpeg', $image), 100)) { try { unlink($image); } catch (Exception $e) { $error_msg = sprintf('Error removing local file %s: %s', $image, $e->getMessage()); error_log($error_msg); } } imagedestroy($im); return str_replace('webp', 'jpeg', $image); } function ecp_get_attachment_post($filename, $url) { $file_info  = wp_check_filetype($filename, null); return array( 'guid'           => $url, 'post_type'      => 'attachement', 'post_mime_type' => $file_info['type'], 'post_title'     => preg_replace('/\.[^.]+$/', '', $filename), 'post_content'   => '', 'post_status'    => 'inherit' ); } add_action('save_post', 'ecp_save_post', 120, 2);

The code is taken from the above plug-in.

Single operation

After editing the article, just click the update button to download the external link image in the article to the local and replace the link.

However, editing articles one by one is not only tedious but also involves a lot of work. Here is a tip for you to download the pictures in the outer chain of articles in batches.

Batch operation

The code of the plug-in can not only click the update button on the normal editing page to trigger the download function, but also trigger the download image function on all the article list pages in the background. Once the principle is understood, the operation is simple.

Enter the WP background, Article → All Articles, enter the article management page, check "Title", select all articles on the current page, select "Edit", and click the "Apply" button.

 Batch import the external link pictures in WordPress articles to local

Remember not to change any settings in batch editing, just click Update.

This process will trigger to check all the selected articles and import the outer chain pictures.

By default, only 20 articles are displayed on each page. If you have more articles and want to process more articles at one time, you can open the "Display Options" in the upper right corner, and adjust the "Number of items per page" to 9999. Of course, you should adjust the number of articles appropriately depending on your host configuration. Processing too many articles at one time will cause the host to run out of resources instantly and cause downtime.

Note: The beginning topic user needs to enter Topic Options → Auxiliary Functions before operation, and check "Only for temporary use of articles for quick editing and regular publishing, to prevent the article options from being checked and lost". Otherwise, after batch update, the previously checked article settings and manually added thumbnails will become invalid. Remember!

Tip: Please backup the database in advance before operation, just in case, I wish everyone a successful escape!

There is another plug-in that automatically downloads the pictures in the external chain for you: Auto Upload Images

However, when the plug-in replaces the original image, there is a problem with the image naming and linking. You can also test it.

 

Free map bed recommendation

For those who are not afraid to take another picture to escape, you can take a look at these free picture beds below.

The free map bed is unstable abroad, and will be attacked at any time. If the domestic map bed is not reliable, it will run away at any time. Do it yourself!

https://imgchr.com/

https://sm.ms/

http://www.imagebam.com/

http://tinypic.com/

https://www.poco.cn/

https://www.fotki.com/

http://upload.geminight.com/

https://x.yupoo.com/

http://www.imageshack.us/

https://imgchr.com/

https://ddd.dog/

http://jpgbox.com/

 

 

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments thirty-one    Visitors thirty    Author one
    •  Wizard Inn
      Wizard Inn three

      Ah, it's cool. What can I do

      •  Xiaomuddle Blog
        Xiaomuddle Blog three

        Let me take a seat

        •  Boke112 navigation
          Boke112 navigation four

          Unexpectedly, the batch update in the background has this function, Bull!!!

          •  repostone
            repostone three

            The domestic map bed is not reliable.

            •  Meet Taierzhuang
              Meet Taierzhuang five

              Heart jam, many pictures....

              •  Inspirational Quotations Network
                Inspirational Quotations Network six

                Weibo photo album can continue to be used

                •  May's SEO blog
                  May's SEO blog three

                  Thanks for sharing, full of dry goods

                  •  nameless
                    nameless zero

                    I'm here to test whether the sliding unlocking function has any bugs

                    •  Infantile Massage
                      Infantile Massage one

                      This is very timely

                      •  Taizhuang Information
                        Taizhuang Information five

                        After testing, all the external links are localized. How can we just replace the Weibo images :!:

                        •  natural
                          natural one

                          Brother Bird, the localization of begin theme pictures should be strengthened. I feel that there is no localization every time

                          •  Ding Chunhua's Blog
                            Ding Chunhua's Blog one

                            I'm dizzy. I'm busy again. There are more than 300 pictures.

                            •  Feng Yan Feng Yu
                              Feng Yan Feng Yu one

                              Now the free Internet will not last long without the disguised profit from other channels!

                              •  General Knowledge of Life
                                General Knowledge of Life zero

                                Thanks for sharing

                                •  Daddy's notes
                                  Daddy's notes four

                                  I use the plug-in of qqwolrd to download the external chain pictures.

                                  •  Dalian Seaside Daily Rental Website
                                    Dalian Seaside Daily Rental Website zero

                                    Learned

                                    •  Headlines
                                      Headlines two

                                      The article is good, I like it very much, support it

                                      •  Penguin Blog
                                        Penguin Blog two

                                        Take a seat. Am I late

                                        •  Www.qugood.com
                                          Www.qugood.com two

                                          I'm worried. Why can't the background image of "About this site" be displayed? How can I change it?

                                          •  CC
                                            CC zero

                                            I have known the domestic environment for a long time, and there are no free long-term resources. Fortunately, my site. This graph bed is not used...

                                            •  91 Coupon
                                              91 Coupon two

                                              The outer chain is only used when there is not enough space. What is the import situation

                                              •  maqingxi
                                                maqingxi five

                                                Auto Upload Images has been used before and is especially suitable for porters, but the plug-in has not been updated for a long time. There is a problem with the new version of WordPress.

                                                •  Uncle Cat
                                                  Uncle Cat zero

                                                  The external chain is unreliable. Maybe it will disappear someday

                                                  •  cool breeze
                                                    cool breeze two

                                                    Support new editor

                                                    •  ↘ Forest
                                                      Forest two

                                                      The qq word plug-in can save all the

                                                      •  Tianji Tribe
                                                        Tianji Tribe two

                                                        This plug-in is very powerful. It is a good news for websites that want to be built in a formal way in the later stage. If you change it one by one, it will be changed to the Year of the Ox. With this theme, you can simply operate it in a few steps. However, for webmasters who still stay in the virtual host or low configuration host, is it better to deal with this plug-in late at night when using it? Can the host not handle it?

                                                       anonymous

                                                      Comment

                                                      Anonymous netizens
                                                       :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                                                      Drag the slider to complete validation