好吧,我找到了答案被黑客開另一個插件。
首先,我通過postmeta表圈
$postid_list = $wpdb->get_results("SELECT distinct post_id FROM yars_postmeta WHERE meta_key='featured_image' ORDER BY post_id DESC LIMIT 10");
if (!$postid_list){
die('No posts with images were found.');
}
foreach ($postid_list as $v) {
$post_id = $v->post_id;
//$options['url_method'] = $url_method;
echo fig_fetch_images($post_id).'<br/>';
}
然後在功能我得到的圖像,然後把它上傳到媒體庫以及設置功能的圖像的帖子ID
function fig_fetch_images($post_id) {
global $wpdb;
//Check to make sure function is not executed more than once on save
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
remove_action('publish_post', 'fetch_images');
//$post = get_post($post_id);
$first_image = '';
$key = 'featured_image';
$first_image = get_post_meta($post_id, $key, true);
$wpdb->query("update yars_postmeta set meta_key ='featured_image_uploaded'WHERE meta_key='featured_image' AND post_id=".$post_id);
if (strpos($first_image,$_SERVER['HTTP_HOST'])===false) {
//Fetch and Store the Image
$get = wp_remote_get($first_image);
$type = wp_remote_retrieve_header($get, 'content-type');
$mirror = wp_upload_bits(rawurldecode(basename($first_image)), '', wp_remote_retrieve_body($get));
//Attachment options
$attachment = array(
'post_title'=> basename($first_image),
'post_mime_type' => $type
);
// Add the image to your media library and set as featured image
$attach_id = wp_insert_attachment($attachment, $mirror['file'], $post_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $first_image);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
// re-hook this function
add_action('publish_post', 'fetch_images');
}
return ('Done post '. $post_id .' : '. $first_image);
}
原來的插件是Hotlink Image Cacher!
WordPress已經啓用了特色圖像功能,您只需要設置它。 –