简介描述
很多人的博客图片大多外链接自新浪微博相册,支持外链,速度快,免费。但是问题也随之而来,东西存在别人家终究不是长久之际免费的产品就更不用说了,最近突然发现文章中的图片无法显示。这不,新浪终于开始动作了,很多网站的图片都无法正常显示了。
通用的解决方法,打开当前主题头部模板header.php在上面最后一个标签下面添加:
<meta name="referrer" content="no-referrer">图片可以正常显示了,但上面的方法只解决了前端图片显示,后台编辑添加图片时还是看不到图片,最终解决方法:将下面代码添加到当前主题函数模板functions.php中:
// 添加referrer标签
add_action('wp_head', 'zm_admin_referrer');// 前端
add_action('admin_head', 'zm_admin_referrer');// 后台
add_action('login_head', 'zm_admin_referrer');// 登录注册页面
function zm_admin_referrer(){
echo'<meta name="referrer" content="no-referrer" />';
}但是上述referrer标签可能会影响百度统计,所以让我很苦恼,最终决定图片搬至自己的服务器。
方法一、Easy Copy Paste
Easy Copy Paste的插件,批量将文章中的外链图片导入到本地。
也可以直接将下面的代码,添加到当前主题函数模板 functions.php 中:
function ecp_save_post($post_id, $post) {
global $wpdb;
if($post->post_status == 'publish') {
$p = '//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); 进入 WP 后台,文章→所有文章,进入文章管理页面,勾选“标题”全选当前页面的所有文章,并选择“编辑”,并点击“应用”按钮。

切记,不要更改批量编辑中的任何设置,只需单击 “更新”即可。这个过程将触发检查所有选定的文章,并导入外链图片。默认每页只显示 20 篇文章,如果你的文章较多,并想一次性处理更多的文章,可以打开右上角的“显示选项”,将“每页的项目数”调整为 9999,当然要视你的主机配置适当调整文章数量,一次性处理太多文章,会让主机瞬间资源耗尽,造成宕机。
方法二、图片搬迁
博主的搬迁方法比较呆,在数据库中搜索带新浪图片链接直接导出

然后用chrome浏览器下载插件批量下载图片(不变更图片名称)

选择快速下载按钮一键下载

图片下载之后,上传至所在的服务器或者OSS储存,然后在数据库修改即可
(例如:https://ws1.sinaimg.cn/large/ 变更 https://www.sbkko.com/wp-content/uploads/large/)




首页的外链缩略图 无法显示 有解决办法吗
谢谢分享哈~~~~