无插件实现wordpress远程图片自动本地化
发布时间:2025-10-19 15:30:02 本站作者 【 字体:大 中 小 】 浏览:116 次
找资料时在其他人的博客看到一篇远程图片自动本地化的代码,分享给大家,将代码写入主题的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;
} 热门推荐
最新发布
酱骨架的家常做法
材料骨架,盐,老抽,白酒做法1.骨架用盐和少许老抽,白酒腌制.不加水2.把腌制好的骨架扔进高压锅,中火,上气后调小火五分钟,关火,焖一会儿再打开锅盖3.接下来要做的就是啃了,喜欢喝酒可以一边喝酒一边啃,很爽吧...
咕噜肉最正宗的做法
咕噜肉又称甜酸肉或咕咾肉,是广东的一道汉族传统名菜,属于粤菜,以甜酸汁及猪肉煮成。这道菜是欧美人士最熟悉的中国菜之一,因此广见于中国以外的唐人街的餐馆。咕噜肉的主料去皮五花猪肉500克,熟鲜笋肉150克,鸡蛋液30克,辣椒25克,葱段5克,蒜泥,芝麻油各5克,精盐1.5克,汾酒7.5克,湿淀粉40克,干淀粉...
东坡肉最正宗的做法
东坡肉(滚肉、红烧肉)是杭州名菜,用猪肉炖制而成。其色、香、味俱佳,深受人们喜爱。慢火,少水,多酒,是制作这道菜的诀窍。一般是一块约二寸许的方正形猪肉,一半为肥肉,一半为瘦肉,入口香糯、肥而不腻,带有酒香,色泽红亮,味醇汁浓,酥烂而形不碎,十分美味。东坡肉的材料猪五花肋肉1000克,葱100克,冰糖...
木须肉最正宗的做法
一点鸡蛋,一点黄瓜,少许肉片,就能做出我很喜欢吃的木须肉了。木须肉的材料猪肉150克,鸡蛋2个,黄瓜50克,木耳5克,葱花、姜末、盐、料酒、香油、味精、淀粉各适量木须肉的做法1.将鸡蛋打入碗中,先取少量蛋清放入一个干净的碗中,再用筷子将剩下的鸡蛋搅打均匀。2.猪肉洗净切成薄片,用刚才预留的...






