无插件实现wordpress远程图片自动本地化(本站亲测)

和上篇文章的代码不一样,我使用的是这篇文章提供的WordPress远程图片自动本地化代码,还没测试效果如何,顺便贴出来,使用方法和上篇文章一样,将代码写入主题的functions.php文件或者是functions.php的引入文件中即可。

代码如下:

/************自动下载外部图片开始**************/
//多级目录创建
function mkdirs($dir){ 
    if(!is_dir($dir)){  
    if(!$this->mkdirs(dirname($dir))){  
     return false;  
    }  
    if(!mkdir($dir,0777, true)){
    return false;  
    } 
    }
    return true;  
} 
 
function save_post_fix($content){
	$post_id = get_the_ID();
	$upload_dir = wp_upload_dir();
	$path = $upload_dir["url"];
	$realPath = $upload_dir["path"];
 
	if(!is_dir($realPath)){
 		mkdirs($realPath);
	}
    $pagelink=array();
	$pattern_page = '/<img([sS]*?)src=\\["|'](.*?)\\["|']([sS]*?)>/i';
	preg_match_all($pattern_page, $content, $pagelink[]);
	foreach ($pagelink[0][2] as $key => $value) {
		$pic_url = $value;
		$url_feter = parse_url($pic_url);
		if(stripos($url_feter["host"],"zhnytech.com")){
			continue;
		}else{
			 $ch = curl_init(); // 启动一个CURL会话
		     curl_setopt($ch,CURLOPT_HEADER,1); //不示返回的Header区域内容
		     curl_setopt($ch, CURLOPT_NOBODY, TRUE);
		     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		     curl_setopt($ch,CURLOPT_URL,$pic_url);
		     $hd = curl_exec($ch);
		     if(!empty($hd) && !(strpos($hd,'Content-Length: image/png')||strpos($hd,'Content-Length: image/jpg'))){
		     	$fp =file_get_contents($pic_url);
		     	$pic_name =basename($url_feter["path"]);
		     	$savePath = $realPath.'/'.$pic_name;
		     	$fullPath = $path.'/'.$pic_name;
		     	if(file_exists($savePath)){
		     		$savePath = $realPath.'/'.str_replace('.','-'.date("s").'.' ,$pic_name);
		     		$fullPath = $path.'/'.str_replace('.','-'.date("s").'.' ,$pic_name);
		     	}
 
		        if(file_put_contents($savePath,$fp)){
		        	$content = str_replace($pic_url, $fullPath, $content);
		        	//插数据库生成预览图
		        	$wp_filetype = wp_check_filetype(basename($savePath), null );
					$wp_upload_dir = wp_upload_dir();
					$attachment = array(
					     'guid' => $wp_upload_dir['url'] . '/' . basename( $savePath ), 
					     'post_mime_type' => $wp_filetype['type'],
					     'post_title' => preg_replace( '/.[^.]+$/', '', basename( $savePath ) ),
					     'post_content' => '',
					     'post_status' => 'inherit'
					);
					$attach_id = wp_insert_attachment( $attachment, $savePath, $post_id );
					require_once( ABSPATH . 'wp-admin/includes/image.php' );
					$attach_data = wp_generate_attachment_metadata( $attach_id, $savePath );
					wp_update_attachment_metadata( $attach_id, $attach_data );
		        }
		    }
 
		}
	}
	return $content;
}
add_filter( 'content_save_pre', 'save_post_fix', 90, 1 );
 
/************自动下载外部图片结束**************/

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有M币奖励和额外收入!

模板下载吧 Wordpress教程 无插件实现wordpress远程图片自动本地化(本站亲测) https://www.mbxzb.com/blog/wordpress/655.html

从明天起,做一个幸福的人,喂马、劈柴、周游世界…

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

评论
暂无评论
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 威武 友军 顶贴
官方客服团队

为您解决烦忧 - 24小时在线 专业服务