WordPress implementation of a summary of the first remote picture as thumbnails and save to the local

Source: Internet
Author: User

In some projects, the site allows users to use the outside of the chain to publish articles, or for some special reasons, the site would like to grab the first remote picture in the text as a feature picture, and like QQ space, the picture saved to the local, and inserted into the database. In short, after the completion of a series of actions, the site hopes to find this is originally remote outside the chain of pictures are placed in the local, and become a feature of the article pictures.

This action we use a hook to achieve:

The code is as follows Copy Code

Add_action (' Publish_post ', ' fetch_images ', 999);

Then create a fetch_images function to implement all of the features described in this article.

The code is as follows Copy Code

function Fetch_images ($post _id) {
    if (defined (' Doing_autosave ') && doing_autosave) return;
& nbsp;   if (!current_user_can (' Edit_post ', $post _id)) return;
    $post = get_post ($post _id);
    .... Next, we want to get the first picture in the content of the article:

    $first _image = ';
    preg_match ('//i ', $post->post_content, $images);
    if (!empty ($images)) foreach ($images as $image) {
         if (Strpos ($image, ' http ') = = 0) {
            $ First_image = $images [1];
            break;
       }
   }


But in fact, the picture Src obtained from the above code may be problematic, or it will not crawl to the data at all. But let's not consider these issues first, we will achieve the objectives of this article first.
The next step is key code one, which implements the "crawl-save to local" two features:

The code is as follows Copy Code
$get = Wp_remote_get ($get _image_src);
$type = Wp_remote_retrieve_header ($get, ' content-type ');
$file _name = basename ($get _image_src);
$file _content = Wp_remote_retrieve_body ($get);
$mirror = Wp_upload_bits ($file _name,null, $file _content);


This place has a change, above we obtained the $first_image, this place's $get_image_src is the above obtains to crawl the remote picture address.

In WordPress, Wp_remote_get, Wp_remote_retrieve_body and other original capture and information acquisition functions, you can view the official documents to understand the remote-related functions. Wp_upload_bits saves the binary content of the captured image locally, according to its file type, and eventually becomes a locally saved picture, saving the local picture information obtained after the save in the $mirror.

Now that you have saved it locally, the next step is to save the picture information to the database.

  code is as follows copy code

$attachment = Array (
    ' post_title ' => $file _name,
    ' post_mime_type ' => $type
) ;
$attach _id = wp_insert_attachment ($attachment, $mirror [' file '], $post _id);
$attach _data = Wp_generate_attachment_metadata ($attach _id, $mirror [' file ']);
Wp_update_attachment_metadata ($attach _id, $attach _data);
Set_post_thumbnail ($post _id, $attach _id);

The various variables used here are obtained in the previous step. This code inserts the information that is saved locally into the database through the attachment correlation function. Through the processing of these functions, but also produced in the attachment related to the various dimensions of the actual picture, such as you set in the background of the small, large, such as the size of the picture, want to understand the WordPress thumbnail, please read the WordPress article features detailed features of the picture feature detailed. And to understand the specific implementation of these functions, you can also read the "Integration Kindeditor to WordPress, especially the image upload part of" a text, which has a detailed introduction to these several attachment functions.

Finally, use the Set_post_thumbnail function to make this attachment a featured image of the article.

Of course, if you need to use this image as a locally preserved feature, it's a good idea to change the image inside the article to a local image. Instead, use a replacement code to replace the corresponding picture address inside the article with a local picture.

The code is as follows Copy Code

$updated = Str_replace ($get _image_src, $mirror [' url '], $post->post_content);
Wp_update_post (Array (' ID ' => $post _id, ' post_content ' => $updated));

Well, this function basically relies on this kind of idea to realize


If it's a local server, simply extract the first picture in the article as a thumbnail

Using code to achieve WordPress crawl article The first picture for the thumbnail function, the following code copied to the theme folder under the functions.php

The code is as follows Copy Code

function Catch_that_image () {

Global $post, $posts;

$first _img = ';

Ob_start ();

Ob_end_clean ();

$output = Preg_match_all ('//i ', $post->post_content, $matches);

$first _img = $matches [1] [0];

if (Empty ($first _img)) {

$first _img = bloginfo (' Template_url '). '/screenshot.png ';

}

return $first _img;

}


Call the following code where you want to display the thumbnail

The code is as follows Copy Code

</a>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.