国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WORDPRESS遠(yuǎn)程圖片本地化實(shí)現(xiàn)程序

2024-09-07 00:51:28
字體:
供稿:網(wǎng)友

wordpress博客發(fā)文章時如果帶有圖片不會自動下載而是使用原網(wǎng)站圖片了,下面我們一起看看WORDPRESS遠(yuǎn)程圖片本地化例子.

將遠(yuǎn)程圖片本地化代碼寫入/wp-content/themes/twentyfourteen/主題的functions.php文件中即可,以后每次在wordpress發(fā)布文章時如果文章中含有外鏈圖片就會自動本地化了,無需任何設(shè)置操作非常方便,代碼如下:

  1. // 遠(yuǎn)程圖片本地化 
  2. add_filter(‘content_save_pre’, ‘auto_save_image’); 
  3. function auto_save_image($content){ 
  4. $upload_dir = wp_upload_dir(date(‘Y/m’)); 
  5. $upload_path = $upload_dir['path']; 
  6. $upload_url_path = $upload_dir['url']; 
  7. require_once (“../wp-includes/class-snoopy.php”); 
  8. $snoopy_Auto_Save_Image = new Snoopy; 
  9. $img = array(); 
  10. if (!emptyempty($_REQUEST['post_title'])) 
  11. $post_title = wp_specialchars(stripslashes($_REQUEST['post_title'])); 
  12. $text = stripslashes($content); 
  13. preg_match_all(“/ src=(\”|\’){0,}(http:\/\/(.+?))(\”|\’|\s)/is”, $text$img); 
  14. $img = array_unique(dhtmlspecialchars($img[2])); 
  15. foreach ($img as $key => $value){ 
  16. set_time_limit(180); //每個圖片最長允許下載時間,秒 
  17. if(str_replace(get_bloginfo(‘url’), “”, $value) == $value && str_replace(get_bloginfo(‘home’), “”, $value) == $value){ 
  18. $fileext = substr(strrchr($value, ‘.’), 1); 
  19. $fileext = strtolower($fileext); 
  20. if($fileext == “” || strlen($fileext) > 4) 
  21. $fileext = “jpg”; 
  22. $savefiletype = array(‘jpg’, ‘gif’, ‘png’, ‘bmp’); 
  23. if (in_array($fileext$savefiletype)){ 
  24. if($snoopy_Auto_Save_Image->fetch($value)){ 
  25. $get_file = $snoopy_Auto_Save_Image->results; 
  26. }else
  27. echo “error fetching file: ” . $snoopy_Auto_Save_Image->error . “<br>”; 
  28. echo “error url: ” . $value
  29. die(); 
  30. $filetime = time(); 
  31. $filepath = “/” . $upload_path//圖片保存的路徑目錄 
  32. $filename = substr($valuestrrpos($value, ‘/’), strrpos($value, ‘.’) – strrpos($value, ‘/’)); 
  33. $fp = @fopen(“..” . $filepath . $filename . “.” . $fileext, “w”); 
  34. @fwrite($fp$get_file); 
  35. fclose($fp); 
  36. $wp_filetype = wp_check_filetype($filename . “.” . $fileext, false); 
  37. $type = $wp_filetype['type']; 
  38. $post_id = (int)$_POST['temp_ID2']; 
  39. $title = $post_title
  40. $url = $upload_url_path . $filename . “.” . $fileext
  41. $file = $_SERVER['DOCUMENT_ROOT'] . $filepath . $filename . “.” . $fileext
  42. $attachment = array(‘post_type’ => ‘attachment’, 
  43. ‘post_mime_type’ => $type
  44. ‘guid’ => $url
  45. ‘post_parent’ => $post_id
  46. ‘post_title’ => $title
  47. ‘post_content’ => ”, 
  48. ); 
  49. $id = wp_insert_attachment($attachment$file$post_parent); 
  50. $text = str_replace($value$url$text); //替換文章里面的圖片地址 
  51. $content = AddSlashes($text); 
  52. remove_filter(‘content_save_pre’, ‘auto_save_image’); 
  53. return $content
  54. function dhtmlspecialchars($string){ 
  55. if(is_array($string)){ 
  56. foreach($string as $key => $val){ 
  57. $string[$key] = dhtmlspecialchars($val); 
  58. //開源軟件:Vevb.com 
  59. }else
  60. $string = str_replace(array(‘&’, ‘”‘, ‘<‘, ‘>’), array(‘&amp;’, ‘&quot;’, ‘&lt;’, ‘&gt;’), $string); 
  61. if(strpos($string, ‘&amp;#’) !== false) { 
  62. $string = preg_replace(‘/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/’, ‘&\\1′, $string); 
  63. return $string
  64. }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 勐海县| 尚义县| 平山县| 长宁县| 内黄县| 肇州县| 朝阳区| 松桃| 普安县| 含山县| 东方市| 高密市| 丁青县| 宁蒗| 望谟县| 左云县| 山阴县| 无锡市| 涿鹿县| 阆中市| 榕江县| 郴州市| 福清市| 鄂托克前旗| 乐陵市| 盱眙县| 乐业县| 砚山县| 顺平县| 桂平市| 阜康市| 崇义县| 云南省| 林芝县| 阳东县| 逊克县| 毕节市| 白城市| 册亨县| 都匀市| 措勤县|