WordPress教程

代码实现wordpress远程图片自动本地化

阿里云

在 wordpress 主题中加入远程图片文件自动本地化代码,经常转载文章的朋友应该会用到这个功能。把外链图片自动保存到网站本地,防止外链图片失效造成图片不显示。把下面的代码添加到主题的 functions.php 文件或者是 functions.php 的引入文件中即可,以后每当在 wordpress 发布文章时如果文章中含有外链图片就会自动本地化了,无需任何设置操作非常方便。

  1.     add_filter('content_save_pre', 'auto_save_image');
  2.     function auto_save_image($content) {
  3.      $upload_path = '';
  4.      $upload_url_path = get_bloginfo('url');
  5.  
  6.      //上传目录
  7.      if (($var = get_option('upload_path')) !=''){
  8.      $upload_path = $var;
  9.      } else {
  10.      $upload_path = 'wp-content/uploads';
  11.      }
  12.      if(get_option('uploads_use_yearmonth_folders')) {
  13.      $upload_path .= '/'.date("Y",time()).'/'.date("m",time());
  14.      }
  15.  
  16.      //文件地址
  17.      if(($var = get_option('upload_url_path')) != '') {
  18.      $upload_url_path = $var;
  19.      } else {
  20.      $upload_url_path = bloginfo('url');
  21.      }
  22.      if(get_option('uploads_use_yearmonth_folders')) {
  23.      $upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
  24.      }
  25.  
  26.      require_once ("../wp-includes/class-snoopy.php");
  27.      $snoopy_Auto_Save_Image = new Snoopy;
  28.  
  29.      $img = array();
  30.  
  31.      //以文章的标题作为图片的标题
  32.      if ( !empty( $_REQUEST['post_title'] ) )
  33.      $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
  34.      $text = stripslashes($content);
  35.      if (get_magic_quotes_gpc()) $text = stripslashes($text);
  36.      preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);
  37.      $img = array_unique(dhtmlspecialchars($img[2]));
  38.      foreach ($img as $key => $value){
  39.      set_time_limit(180); //每个图片最长允许下载时间,秒
  40.      if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
  41.      //判断是否是本地图片,如果不是,则保存到服务器
  42.      $fileext = substr(strrchr($value,'.'),1);
  43.      $fileext = strtolower($fileext);
  44.      if($fileext==""||strlen($fileext)>4)
  45.      $fileext = "jpg";
  46.      $savefiletype = array('jpg','gif','png','bmp');
  47.      if (in_array($fileext, $savefiletype)){
  48.      if($snoopy_Auto_Save_Image->fetch($value)){
  49.      $get_file = $snoopy_Auto_Save_Image->results;
  50.      }else{
  51.      echo "error fetching file: ".$snoopy_Auto_Save_Image->error."<br>";
  52.      echo "error url: ".$value;
  53.      die();
  54.      }
  55.      $filetime = time();
  56.      $filepath = "/".$upload_path;//图片保存的路径目录
  57.      !is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
  58.      //$filename = date("His",$filetime).random(3);
  59.      $filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
  60.  
  61.      //$e = '../'.$filepath.$filename.'.'.$fileext;
  62.      //if(!is_file($e)) {
  63.      // copy(htmlspecialchars_decode($value),$e);
  64.      //}
  65.      $fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
  66.      @fwrite($fp,$get_file);
  67.      fclose($fp);
  68.  
  69.  
  70.  
  71.      $wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
  72.      $type = $wp_filetype['type'];
  73.      $post_id = (int)$_POST['temp_ID2'];
  74.      $title = $post_title;
  75.      $url = $upload_url_path.$filename.".".$fileext;
  76.      $file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
  77.  
  78.      //添加数据库记录
  79.      $attachment = array(
  80.      'post_type' => 'attachment',
  81.      'post_mime_type' => $type,
  82.      'guid' => $url,
  83.      'post_parent' => $post_id,
  84.      'post_title' => $title,
  85.      'post_content' => '',
  86.      );
  87.      $id = wp_insert_attachment($attachment, $file, $post_parent);
  88.      $text = str_replace($value,$url,$text); //替换文章里面的图片地址
  89.      }
  90.      }
  91.      }
  92.      $content = AddSlashes($text);
  93.      remove_filter('content_save_pre', 'auto_save_image');
  94.      return $content;
  95.     }
  96.  
  97.     function mkdirs($dir)
  98.     {
  99.      if(!is_dir($dir))
  100.      {
  101.      mkdirs(dirname($dir));
  102.      mkdir($dir);
  103.      }
  104.      return ;
  105.     }
  106.     function dhtmlspecialchars($string) {
  107.      if(is_array($string)) {
  108.      foreach($string as $key => $val) {
  109.      $string[$key] = dhtmlspecialchars($val);
  110.      }
  111.      }else{
  112.      $string = str_replace('&', '&', $string);
  113.      $string = str_replace('"', '"', $string);
  114.      $string = str_replace('<', '<', $string);
  115.      $string = str_replace('>', '>', $string);
  116.      $string = preg_replace('/&(#\d;)/', '&\1', $string);
  117.      }
  118.      return $string;
  119.     }
也想出现在这里?联系我们
创客主机

代码实现 wordpress 远程图片自动本地化

已有 903 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!