WordPress代码实现 外链图片 自动本地化

WordPress的许多插件或代码可以在编辑文章时自动将外部图片下载到本地。 最后,我选择了一个名为Easy Copy Paste的插件。

单个操作
之后,您只需单击更新按钮即可编辑文章,以将文章中的外部链接图像下载到本地并替换链接。
但是,逐篇编辑文章不仅麻烦,而且工作量很大。 这是您可以批量下载文章中外部图片的一个小技巧。
批处理操作
该插件的代码不仅可以通过单击常规编辑页面上的更新按钮来触发下载功能,而且还可以在其中的所有文章列表页面中触发下载图像功能。 背景。 原理被理解并且操作很简单Up。
进入WP后台,文章→所有文章,进入文章管理页面,勾选“标题”全选当前页面的所有文章,并选择“编辑”,并点击“应用”按钮。

WordPress代码实现 外链图片 自动本地化-橘子皮

请记住,不要在批处理编辑中更改任何设置,只需单击“更新”。
此过程将触发以检查所有选定的文章并导入外部链接。
默认情况下,每页仅显示20条文章。 如果您有更多文章并且想要一次处理更多文章,则可以打开右上角的“显示选项”,并将“每页的项目数”设置为9999。当然,您需要调整数量 适当的文章取决于您的主机配置。 一次处理过多的文章会立即耗尽主机的资源,并导致停机。

代码版:

也可以直接将下面的代码,添加到当前主题函数模板 functions.php 中:

    function ecp_save_post($post_id, $post) {
    	global $wpdb;
    	if($post->post_status == 'publish') {
    		$p   = '/<img.*[\s]src=[\"|\'](.*)[\"|\'].*>/iU';
    		$num = preg_match_all($p, $post->post_content, $matches);
    		if ($num) {
    			$wp_upload_dir = wp_upload_dir();
    			set_time_limit(0);
    			$ch = curl_init();
    			curl_setopt($ch, CURLOPT_HEADER, false);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    			curl_setopt($ch, CURLOPT_MAXREDIRS,20);
    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
     
    			$ecp_options = $_SERVER['HTTP_HOST'];
    			foreach ($matches[1] as $src) {
    				if (isset($src) && strpos($src, $ecp_options) === false) {
    					$file_info = wp_check_filetype(basename($src), null);
    					if ($file_info['ext'] == false) {
    						date_default_timezone_set('PRC');
    						$file_name = date('YmdHis-').dechex(mt_rand(100000, 999999)).'.tmp';
    					} else {
    						$file_name = dechex(mt_rand(100000, 999999)) . '-' . basename($src);
    					}
    					curl_setopt($ch, CURLOPT_URL, $src);
    					$file_path = $wp_upload_dir['path'] . '/' . $file_name;
    					$img = fopen($file_path, 'wb');
    					curl_setopt($ch, CURLOPT_FILE, $img);
    					$img_data  = curl_exec($ch);
    					fclose($img);
     
    					if (file_exists($file_path) && filesize($file_path) > 0) {
    						$t   = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    						$arr = explode('/', $t);
    						if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp') {
    							$file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'tmp');
    						} elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp') {
    							$file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'webp');
    						}
    						$post->post_content  = str_replace($src, $wp_upload_dir['url'] . '/' . basename($file_path), $post->post_content);
    						$attachment = ecp_get_attachment_post(basename($file_path), $wp_upload_dir['url'] . '/' . basename($file_path));
    						$attach_id = wp_insert_attachment($attachment, ltrim($wp_upload_dir['subdir'] . '/' . basename($file_path), '/'), 0);
    						$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
    						$ss = wp_update_attachment_metadata($attach_id, $attach_data);
    					}
    				}
    			}
    			curl_close($ch);
    			$wpdb->update( $wpdb->posts, array('post_content' => $post->post_content), array('ID' => $post->ID));
    		}
    	}
    }
     
    function ecp_handle_ext($file, $type, $file_dir, $file_name, $ext) {
    	switch ($ext) {
    		case 'tmp':
    			if (rename($file, str_replace('tmp', $type, $file))) {
    				if ('webp' == $type) {
    					return ecp_image_convert('webp', 'jpeg', $file_dir . '/' . str_replace('tmp', $type, $file_name));
    				}
    				return $file_dir . '/' . str_replace('tmp', $type, $file_name);
    			}
    		case 'webp':
    			if ('webp' == $type) {
    				return ecp_image_convert('webp', 'jpeg', $file);
    			} else {
    				if (rename($file, str_replace('webp', $type, $file))) {
    					return $file_dir . '/' . str_replace('webp', $type, $file_name);
    				}
    			}
    		default:
    			return $file;
    	}
    }
     
    function ecp_image_convert($from='webp', $to='jpeg', $image) {
    	$im = imagecreatefromwebp($image);
    	if (imagejpeg($im, str_replace('webp', 'jpeg', $image), 100)) {
    		try {
    			unlink($image);
    		} catch (Exception $e) {
    			$error_msg = sprintf('Error removing local file %s: %s', $image,
    				$e->getMessage());
    			error_log($error_msg);
    		}
    	}
    	imagedestroy($im);
     
    	return str_replace('webp', 'jpeg', $image);
    }
     
    function ecp_get_attachment_post($filename, $url) {
    	$file_info  = wp_check_filetype($filename, null);
    	return array(
    		'guid'           => $url,
    		'post_type'      => 'attachement',
    		'post_mime_type' => $file_info['type'],
    		'post_title'     => preg_replace('/\.[^.]+$/', '', $filename),
    		'post_content'   => '',
    		'post_status'    => 'inherit'
    	);
    }
    add_action('save_post', 'ecp_save_post', 120, 2);

 

主题测试文章,只做测试使用。发布者:小屋,转转请注明出处:http://www.rumenwu.com/site/1864.html

(10)
小屋的头像小屋
上一篇 2022年4月10日 下午4:58
下一篇 2022年4月24日 下午6:15

相关推荐

  • 国家互联网应急中心发布美网络攻击我国某先进材料设计研究院事件调查报告

    1 月 17 日消息,国家互联网应急中心刚刚发布了《美网络攻击我国某先进材料设计研究院事件调查报告》,IT之家附原文如下: 2024 年 12 月 18 日,国家互联网应急中心 CNCERT 发布公告,发现处置两起美对我大型科技企业机构网络攻击事件。本报告将公布对其中我国某先进材料设计研究院的网络攻击详情,为全球相关国家、单位有效发现和防范美网络攻击行为提供…

    2025年1月17日
    43100
  • 升级WordPress 5.0后,切换回经典编辑器Classic Editor

    升级WordPress 5.0后,切换回经典编辑器Classic Editor WordPress 5.0 正式采用了全新的区块编辑器-古德堡编辑器Block Editor Gutenberg,对于很多已经习惯国产编辑器习惯的站长来说不是特别方便,下面我们看看如何切换成经典编辑器吧。   相信有不少人不太习惯新编辑器的使用,那么在更新了WordPress 5…

    2018年12月15日
    91600
  • Apache伪静态规则文件.htaccess的另类用法

    1、开启/关闭拼写检查(关闭后会区分大小写) CheckSpelling On #开启 CheckSpelling Off #关闭 关闭拼写检查可以解决一下报错 2、禁止/只允许特定IP访问 (1) 禁止单个IP访问,例如192.168.1.1 Order Allow,Deny Allow from all Deny from 192.168.1.1 (2)…

    建站入门 2019年10月6日
    80300
  • SEO需要掌握哪些基本SEO技巧?

    导航请确保你的网站导航都是以html的形式链接。所有页面之间应该有广泛的互联,如果无法实现这一点,可以考虑建立一个网站地图。 首页网站的首页(home或index页等)应该采用文本的形式,而不是flash等。这个文本里面要包含你的目标关键字或目标短语。 标签<title> < /title>这是标题标签,这里面应当包含你最重要的目标关…

    建站入门 2018年1月21日
    90500
  • 关键字也有权重吗?

    这似乎是一个很新的概念。但,对SEO颇懂行的人就明白,搜索引擎显然会这样做的。 决定关键字权重的大参数(之所以用大参数,是因为小参数还很多): 1、这个网页的PR值、这个网页主域名的PR值; 2、关键字在网页标题中的比重(就是比例)和位置; 3、关键字在meta标签中是否被提及; 4、网页内是否有很多与该关键字相关的关键字词组; 5、关键字在网页上的密度(3…

    建站入门 2018年4月19日
    66200

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信