WordPress自动保存远程图片

这是一个给WordPress编辑器增加“自动保存远程图片”功能的方法。

自动保存远程图片修改方法如下:

1、在wp-admin/edit-form-advanced.php 第157行

<span id="autosave"></span>

来源:http://www.in6.net/blogging/2007/04/22/40/

里面增加

Save pic:<input type="checkbox" name="savepic" value="checked" />

2、修改 wp-admin/post.php,将第7行的

wp_reset_vars(array('action', 'safe_mode', 'withcomments', 'posts', 'content', 'edited_post_title', 'comment_error', 'profile', 'trackback_url', 'excerpt', 'showcomments', 'commentstart', 'commentend', 'commentorder'));

改为

wp_reset_vars(array('action', 'safe_mode', 'withcomments', 'posts', 'content', 'edited_post_title', 'comment_error', 'profile', 'trackback_url', 'excerpt', 'showcomments', 'commentstart', 'commentend', 'commentorder', 'savepic'));

3、在wp-admin/admin-functions.php 的开头增加

function open($file,$type=''){
      
global $fromurl,$referer;
      
$cachename=$file;
              
if($type){
                      
$file=$fromurl.'/'.$type.'/'.$file;
              
}else{
                      
$file=$fromurl.$file;
              
}
                
if($open=file($file)){
                                      
$count=count($open);
                                      
for($i=0;$i<$count;$i++){
                                              
$theget.=$open[$i];
                                      
}                                     
                              
}else{
                                      
die('请求过多,超时,请刷新');
                              
}             
      
return $theget;
}
 
function cut($file,$from,$end){
 
        
$message=explode($from,$file);
        
$message=explode($end,$message[1]);
return        $message[0];
}
function dhtmlspecialchars($string) {
 
if(is_array($string)) {
 
foreach($string as $key => $val) {
  
$string[$key] = dhtmlspecialchars($val);
 
}
 
} else {
 
$string = str_replace('&', '&amp;', $string);
 
$string = str_replace('"', '&quot;', $string);
 
$string = str_replace('<', '&lt;', $string);
 
$string = str_replace('>', '&gt;', $string);
 
$string = preg_replace('/&amp;(#\d;)/', '&\1', $string);
 
}
 
return $string;
}
function random($length) {
 
$hash = '';
 
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
 
$max = strlen($chars) - 1;
 
mt_srand((double)microtime() * 1000000);
 
for($i = 0; $i < $length; $i++) {
 
$hash .= $chars[mt_rand(0, $max)];
 
}
 
return $hash;
}
 
function mkdirs($dir)
{
        
if(!is_dir($dir))
        
{
                
mkdirs(dirname($dir));
                
mkdir($dir);
        
}
        
return ;
}

再在

$_POST['post_content'] = $_POST['content'];

的后面添加以下代码:

if($_POST['savepic']=="checked"){
// begin to save pic;
$img_array = array();
$content1 = stripslashes($_POST['post_content']);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);
//echo $content1;//文章内容嘿嘿开始处理了
preg_match_all("/(src|SRC)=\"(http:\/\/(.+).(gif|jpg|jpeg|bmp|png))/isU",$content1,$img_array);//正则开始匹配所有的图片并放入数据
$img_array = array_unique(dhtmlspecialchars($img_array[2]));
 
print_r($img_array);
set_time_limit(0);
 
 
foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);//开始获取图片了哦
$filetime = time();
$filepath = "../wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/";//图片保存的路径目录
!
is_dir($filepath) ? mkdirs($filepath) : null;
$filename = date("YmdHis",$filetime).random(1).'.'.substr($value,-3,3);
$fp = @fopen($filepath.$filename,"w");
@
fwrite($fp,$get_file);
fclose($fp);//完工,哈
$content1 = preg_replace("/".addcslashes($value,"/")."/isU", "/wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/".$filename, $content1);   //顺便替换一下文章里面的图片地址
 
}
 
$_POST['post_content'] = $content1;
 
// end save pic;
}

完毕。

现在,你再发表文章时,可以看到下图

选中“save pic”,再点“发表”,即可保存远程图片了。

总结一下,要修改的文件有:

edit-form-advanced.php
post.php
admin-functions.php

来源:http://www.in6.net/blogging/2007/04/22/40/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注