修改使用文章远程发布接口发布得文章无法自动获取远程图片问题

鲲V5 发布于 分类:分享

打开文件  wp-content\themes\justnews\functions.php

查找代码:

  if(is_singular('post') && in_the_loop() && is_main_query() && isset($options['expand_more']) && $options['expand_more'] == '1'){
$content .= '<div class="entry-readmore"><div class="entry-readmore-btn"></div></div>';
}
在下面增加:
 

    if (isset($options['save_remote_img']) && $options['save_remote_img'] == '1') {
        preg_match_all('/<img[^>]*src=[\'"]([^\'"]+)[\'"].*>/iU', $content, $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $image_url) {
                // 获取远程图片
                $image = file_get_contents($image_url);
     
                // 生成本地文件名
                $upload_dir = wp_upload_dir();
                $filename = basename($image_url);
                $local_image_path = $upload_dir['path'] . '/' . $filename;
     
                // 保存图片到本地
                file_put_contents($local_image_path, $image);
     
                // 替换文章中的远程图片链接为本地链接
                $content = str_replace($image_url, $upload_dir['url'] . '/' . $filename, $content);
            }
        }
    }
这样后台如果开启了  保存远程图片 得情况 使用接口发布得文章也会自动将文章内得图片下载到本地了。
 
 

1个回复

  • Lomu
    Lomu

    不是很建议这样做,您的代码会在每一次访问文章的时候都保存1次,会有问题的。

    目前主题是通过文章发布操作来触发图片的本地化保存,对于远程发布的文章如果绕开了系统的内容发布流程会出现无法保存的问题,这种一般建议发布的时候改为定时发布,比如1分钟后,这种就能基于系统的文章发布解决图片保存问题。

微信咨询
QQ咨询
扫码关注

扫码关注微信

关注我们获取最新资讯 关注微信公众号 立即扫码关注我们