无插件实现wordpress远程图片自动本地化
发布时间:2025-10-19 15:30:02 本站作者 【 字体:大 中 小 】 浏览:163 次
找资料时在其他人的博客看到一篇远程图片自动本地化的代码,分享给大家,将代码写入主题的functions.php文件或者是functions.php的引入文件中即可,以后每当在wordpress发布文章时如果文章中含有外链图片就会自动本地化了,无需任何设置操作非常方便。
代码如下:
add_filter('content_save_pre', 'auto_save_image');
function auto_save_image($content) {
$upload_path = '';
$upload_url_path = get_bloginfo('url');
//上传目录
if (($var = get_option('upload_path')) !=''){
$upload_path = $var;
} else {
$upload_path = 'wp-content/uploads';
}
if(get_option('uploads_use_yeArmonth_folders')) {
$upload_path .= '/'.date("Y",time()).'/'.date("m",time());
}
//文件地址
if(($var = get_option('upload_url_path')) != '') {
$upload_url_path = $var;
} else {
$upload_url_path = bloginfo('url');
}
if(get_option('uploads_use_yearmonth_folders')) {
$upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());
}
require_once ("../wp-includes/class-snoopy.php");
$snoopy_Auto_Save_Image = new Snoopy;
$img = array();
//以文章的标题作为图片的标题
if ( !empty( $_REQUEST['post_title'] ) )
$post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
$text = stripslashes($content);
if (get_magic_quotes_gpc()) $text = stripslashes($text);
preg_match_all("/ src=("|'){0,}(http://(.+?))("|'|s)/is",$text,$img);
$img = array_unique(dhtmlspecialchars($img[2]));
foreach ($img as $key => $value){
set_time_limit(180); //每个图片最长允许下载时间,秒
if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){
//判断是否是本地图片,如果不是,则保存到服务器
$fileext = substr(strrchr($value,'.'),1);
$fileext = strtolower($fileext);
if($fileext==""||strlen($fileext)>4)
$fileext = "jpg";
$savefiletype = array('jpg','gif','png','bmp');
if (in_array($fileext, $savefiletype)){
if($snoopy_Auto_Save_Image->fetch($value)){
$get_file = $snoopy_Auto_Save_Image->results;
}else{
echo "error fetching file: ".$snoopy_Auto_Save_Image->error."<br>";
echo "error url: ".$value;
die();
}
$filetime = time();
$filepath = "/".$upload_path;//图片保存的路径目录
!is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;
//$filename = date("His",$filetime).random(3);
$filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));
//$e = '../'.$filepath.$filename.'.'.$fileext;
//if(!is_file($e)) {
// copy(htmlspecialchars_decode($value),$e);
//}
$fp = @fopen("..".$filepath.$filename.".".$fileext,"w");
@fwrite($fp,$get_file);
fclose($fp);
$wp_filetype = wp_check_filetype( $filename.".".$fileext, false );
$type = $wp_filetype['type'];
$post_id = (int)$_POST@['temp_ID2'];
$title = $post_title;
$url = $upload_url_path.$filename.".".$fileext;
$file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;
//添加数据库记录
$attachment = array(
'post_type' => 'attachment',
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => '',
);
$id = wp_insert_attachment($attachment, $file, $post_parent);
$text = str_replace($value,'/wp-content/uploads/'.$url,$text); //替换文章里面的图片地址
}
}
}
$content = AddSlashes($text);
remove_filter('content_save_pre', 'auto_save_image');
return $content;
}
function mkdirs($dir)
{
if(!is_dir($dir))
{
mkdirs(dirname($dir));
mkdir($dir);
}
return ;
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
}else{
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = preg_replace('/&(#d;)/', '&1', $string);
}
return $string;
} 热门推荐
最新发布
烹饪技巧-厨房生活小常识
①. 煮鸡蛋不裂壳煮鸡蛋时,在水中加入少许盐或白醋,可以增加水的密度,减少蛋壳碰撞破裂的概率。此外,用冷水下锅,小火慢煮,也能让鸡蛋受热均匀,避免爆裂。②. 快速解冻肉类忘记提前解冻肉类?可以将冷冻肉用铝箔纸包裹,或者放在不锈钢盆上(金属导热快),解冻速度比常温放置快2倍。也可以用冷水浸泡...
如何保养家庭地板
地板保养需注重日常清洁、定期护理、温湿度控制及防护措施的综合管理,具体方法包括使用专用工具清洁、避免过度潮湿、防晒防刮擦等。具体保养方法如下:一.日常清洁1.每日除尘:使用静电除尘拖把或微纤维拖把清除灰尘和毛发,避免刮伤地板。2.污渍处理:轻微污渍用拧干的软布擦拭,顽固污渍可...
厨房装修5种超实用布局
厨房是家的心脏,合理的布局不仅能提升烹饪效率,还能让家人在这里感受到更多的温暖和幸福。今天,就让我们一起看看5种超实用的厨房布局,帮你打造理想的厨房空间。一、U型厨房U型厨房橱柜环绕三面墙壁,收纳功能强大,能轻松收纳各种厨具餐具。操作流程合理,洗菜、切菜、炒菜区域划分明确,烹饪更加得心应手。...
厨房异味太重如何去除?
厨房是家庭中最常发生异味的地方,由于细菌和油烟在空气中徘徊,难免会产生令人不舒适的气味。如果不及时去除厨房中的异味,不仅会影响食欲,还会影响生活质量。那么该如何去除厨房中的异味呢?接下来我将为您提供几种去除厨房异味的方法。1. 使用醋:从多方面来看,醋是一种最有效的清洁剂之一。醋因其较高的酸度值...






