全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 3056|回复: 36

大佬推荐个wp远程图片 本地化的插件

[复制链接]
发表于 2019-3-22 23:50:30 | 显示全部楼层 |阅读模式
之前大佬说的QQWorld自动保存图片插件 不知怎么回事用不了

其他的插件都是发布文章才能自动保存的、用火车头远程发布就不行了

大佬推荐个能远程发布的呀
发表于 2019-3-23 10:57:10 | 显示全部楼层
easy-copy-paste   好用的不得鸟。
 楼主| 发表于 2019-3-23 09:48:52 | 显示全部楼层
yumijie 发表于 2019-3-23 07:02
不要用插件,有纯代码

用了这段代码  图片确实上传到本地了  内容替换有问题
只有图片路径的后面,域名/wp-content/uploads没加上去

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

链接:https://pan.baidu.com/s/1mC9axzarM6AOFCfcPOUKNg  密码:sx7z
发表于 2019-3-23 00:35:11 | 显示全部楼层
不用啊 你找个发布模块就行
发表于 2019-3-23 00:18:35 | 显示全部楼层
火车头你采回来图片 发布了就行啊
 楼主| 发表于 2019-3-23 00:25:45 | 显示全部楼层
ihsky 发表于 2019-3-23 00:18
火车头你采回来图片 发布了就行啊

火车头采图片要fTP上传应该   不方便呀
发表于 2019-3-23 02:32:18 | 显示全部楼层
我用autopost pro采的,功能没火车头那么强大,关键简单好上手,直接支持远程图片保存到媒体库。


发表于 2019-3-23 04:02:29 来自手机 | 显示全部楼层
siyi 发表于 2019-3-23 02:32
我用autopost pro采的,功能没火车头那么强大,关键简单好上手,直接支持远程图片保存到媒体库。



你这是开心版吗

点评

开心,当然要开心啦,不然一年要499呢。不过有点缺陷,不知道是开心版的bug还是本身的问题。能折腾火车头是最好的选择。我也在学习怎么写规则。  发表于 2019-3-23 09:17
发表于 2019-3-23 04:36:01 | 显示全部楼层
siyi 发表于 2019-3-23 02:32
我用autopost pro采的,功能没火车头那么强大,关键简单好上手,直接支持远程图片保存到媒体库。

來個下載連結
发表于 2019-3-23 07:02:50 来自手机 | 显示全部楼层
不要用插件,有纯代码
发表于 2019-3-23 09:02:12 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-27 02:08 , Processed in 0.084461 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表