Loading... ### 函数名称 `upload_external_images` > 位置 \wp-content\plugins\wpjam-basic\includes\class-wpjam-post.php > > line 424 ### 新函数 ```php public function upload_external_images(){ $content = $this->content; // 获取文章内容 $has_error = false; // 初始化一个变量来跟踪是否有错误 // 处理文章内容中的图片 if(preg_match_all('/<img.*?src=[\'"](.*?)[\'"].*?>/i', $content, $matches)){ $img_urls = array_unique($matches[1]); // 获取所有唯一的图片URL $replace = wpjam_fetch_external_images($img_urls, $this->id); // 上传外部图片 if($replace){ $this->save(['post_content' => str_replace($img_urls, $replace, $content)]); // 替换文章中的旧图片URL } else { $has_error = true; // 设置错误标志 } } // 处理特色图 $featured_img_id = \get_post_thumbnail_id($this->id); // 获取特色图ID if($featured_img_id){ $featured_img_url = \wp_get_attachment_url($featured_img_id); // 通过ID获取URL // 检查特色图URL是否是外部链接 if(wpjam_is_external_url($featured_img_url, 'fetch')){ $new_featured_img = wpjam_fetch_external_images(array_unique([$featured_img_url]), $this->id); // 上传外部特色图 if($new_featured_img && isset($new_featured_img[0])){ // 从新的URL获取图片ID $new_featured_img_id = attachment_url_to_postid($new_featured_img[0]); // 更新特色图 \set_post_thumbnail($this->id, $new_featured_img_id); } else { $has_error = true; // 设置错误标志 } } } // 如果有错误,返回WP_Error对象 if($has_error){ return new WP_Error('error', '有一些外部图片没有成功上传'); }else{ return true; } } ``` > **WPJAM BASIC 6.3.2版本** > > 路径:components下 ```php /*newfuns*/ public function upload_external_images($post_id){ $object = wpjam_post($post_id); $content = $object->content; $has_error = false; // 初始化一个变量来跟踪是否有错误 // 处理文章内容中的图片 if(preg_match_all('/<img.*?src=[\'"](.*?)[\'"].*?>/i', $content, $matches)){ $img_urls = array_unique($matches[1]); // 获取所有唯一的图片URL $replace = wpjam_fetch_external_images($img_urls, $post_id); // 上传外部图片 if($replace){ $object->save(['post_content' => str_replace($img_urls, $replace, $content)]); // 替换文章中的旧图片URL } else { $has_error = true; // 设置错误标志 } } // 处理特色图 $featured_img_id = \get_post_thumbnail_id($post_id); // 获取特色图ID if($featured_img_id){ $featured_img_url = \wp_get_attachment_url($featured_img_id); // 通过ID获取URL // 检查特色图URL是否是外部链接 if(wpjam_is_external_url($featured_img_url, 'fetch')){ $new_featured_img = wpjam_fetch_external_images(array_unique([$featured_img_url]), $post_id); // 上传外部特色图 if($new_featured_img && isset($new_featured_img[0])){ // 从新的URL获取图片ID $new_featured_img_id = attachment_url_to_postid($new_featured_img[0]); // 更新特色图 \set_post_thumbnail($post_id, $new_featured_img_id); } else { $has_error = true; // 设置错误标志 } } } return true; // 如果有错误,返回WP_Error对象 if($has_error){ return new WP_Error('error', '有一些外部图片没有成功上传'); }else{ return true; } } /*newfune*/ ``` > 如果需要更新自定义字段 ``` // 你想要添加或更新的自定义字段 $custom_field_key = 'fifu_image_url'; $custom_field_value = $new_featured_img[0]; // 更新自定义字段 update_post_meta($post_id, $custom_field_key, $custom_field_value); ``` 最后修改:2025 年 10 月 14 日 © 允许规范转载 打赏 赞赏作者 微信 赞 0 如果觉得我的文章对你有用,请随意赞赏
此处评论已关闭