無插件實現wordpress遠程圖片自動本地化(本站親
導讀:建站技術建站技術和上篇文章的代碼不一樣,我使用的是這篇文章提供的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遠程圖片自動本地化(本站親主要講述插件,圖片,建站網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_35317.html