最新活动:电脑PC端+手机端+微网站+自适应网页多模板选择-建站388元起价!!!
要源码
当前位置:主页 > 网站建设 > 无插件实现wordpress远程图片自动本地化(本站亲

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

时间:2023-07-23 11:07:23 阅读: 文章分类: 网站建设 作者: 网站开发者

导读:建站技术建站技术和上篇文章的代码不一样,我使用的是这篇文章提供的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_pos织梦模板t_fix', 90, 1 ); /************自动下载外部图片结束**************/相关建站技术网站建设技术。

关键词标签: 建站 图片 插件

声明: 本文由我的SEOUC技术文章主页发布于:2023-07-23 ,文章无插件实现wordpress远程图片自动本地化(本站亲主要讲述插件,图片,建站网站建设源码以及服务器配置搭建相关技术文章。转载请保留链接: https://www.seouc.com/article/web_35317.html

我的IDC 网站建设技术SEOUC.COM
专注网站建设,SEO优化,小程序设计制作搭建开发定制网站等,数千家网站定制开发案例,网站推广技术服务。
  • 5000+合作客服
  • 8年从业经验
  • 150+覆盖行业
  • 最新热门源码技术文章