轻松实现外链图片导入的功能!
嘿,朋友们!今天我要跟你们分享一个小技巧,教你如何在WordPress中轻松导入外链图片。只要把下面这段代码放到你的 functions.php 文件中,就能让你的帖子自动抓取外部图片啦!
function ecp_save_post($post_id, $post) {
global $wpdb;
if($post->post_status == 'publish') {
$p = '//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));
}
}
}
这个代码块其实是个小助手,专门用来处理你帖子里的外链图片。当你发布一篇新文章时,它会自动检查文章内容,找出所有的外部图片链接,然后把它们下载到你的服务器上,真是太贴心了!
接下来,你还需要加入一些辅助函数来处理文件的扩展名和格式转换,下面这些代码也要放到同一个 functions.php 文件里:
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' => '',
);
}
这些代码负责处理图片扩展名的转换和删除多余的文件。这样一来,你的外链图片就能顺利变成你网站中的一份子,省去了很多麻烦。
所以,不妨试试把这些代码添加进你的项目中吧!你会发现,处理外链图片比你想象的简单多了!
轻松导入外链图片的秘诀
其实呢,处理外链图片的时候,可能会遇到一些麻烦,但别担心,这里有个简单的方法!通过添加一些代码,你可以轻松地把外链图片转化为自己网站的一部分,省去不少麻烦。
说白了,代码的主要作用就是处理图片的扩展名,让多余的文件名变得简洁。这样一来,你的外链图片就能顺畅地融入到网站中了。是不是听起来很简单?
所以,为什么不试试看把这些小代码整合到你的项目里呢?你会发现,管理外链图片其实比你一开始想象的要简单得多哦!
来源:知乎
原文标题:如何在wordpress中批量插入图片外链? – 春软 的回答
声明:
文章来自网络收集后经过ai改写发布,如不小心侵犯了您的权益,请联系本站删除,给您带来困扰,深表歉意!
参考文章:淘宝插件狂潮来袭!WordPress用户如何轻松下载实现完美商城?
参考文章:WordPress插件授权遇难题?业内大佬支招破局
参考文章:WordPress插件经验-如何选择适合你的WordPress插件?
本文标题:轻松掌握:WordPress中批量插入图片外链的技巧分享
网址:https://www.wpjiguang.cn/archives/46366.html
本站所有文章由wordpress极光ai post插件通过chatgpt写作修改后发布,并不代表本站的观点;如果无意间侵犯了你的权益,请联系我们进行删除处理。
如需转载,请务必注明文章来源和链接,谢谢您的支持与鼓励!






