给WordPress编辑器增加“自动保存远程图片”功能的方法: 自动保存远程图片修改方法如下: 1、在wp-admin/edit-form-advanced.php 第167行 HTML: <span id="autosave"></span> 后面加增加 HTML: Save pic:<input type="checkbox" name="savepic" value="checked" /> 2、修改 wp-admin/post.php,将第7行的 PHP: wp_reset_vars(array('action', 'safe_mode', 'withcomments', 'posts', 'content', 'edited_post_title', 'comment_error', 'profile', 'trackback_url', 'excerpt', 'showcomments', 'commentstart', 'commentend', 'commentorder')); 改为 PHP: 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 的开头增加 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('&', '&', $string); $string = str_replace('"', '"', $string); $string = str_replace('<', '<', $string); $string = str_replace('>', '>', $string); $string = preg_replace('/&(#\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 ; } 再在 PHP: $_POST['post_content'] = $_POST['content']; 的后面添加以下代码: 注意,上面的代码有两个地方有,要在最后那个的后面加 PHP: 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
老鬼,问个问题~ 我用的是WP2.3.1,按你的方法加进代码,其中把后面那两段加进了wp-admin\includes\post.php里 并且打开了PHP.INI的output_buffering 发布文章,程序会在我指定的目录建立图片,文章内的相关图片地址也会自动更改 但是....有一个很重要的问题........ ............上传的所有图片大小都为0............请问怎么解决呢?先谢过了~