之前分享过一篇 使用 Featured Image from URL 插件设置特色图片为外链图片 的文章,详细介绍了 Featured Image from URL (FIFU) 这个插件的使用场景和方法,今天简单分享一下使用 Blogsite 主题时,使用该插件需要优化的几个地方。

前言
Blogsite 主题中,在很多地方使用了特色图片,这些信息都存储在数据库当中。关于 Blogsite 主题的介绍,可能参考 BlogSite 时尚大气 wordpress 主题 适合搭建个人博客 这篇文章,在该主题中,如果想使用外链图片,可以使用 Featured Image from URL 这个插件,具体使用方法可以参考 使用 Featured Image from URL 插件设置特色图片为外链图片 这篇文章。在 Blogsite 主题中,使用 Featured Image from URL 插件会存在一些不完善的地方,需要优化完善一下,具体参考以下内容。
修改内容
1. 修改 meta-box.php 文件
修改 featured-image-from-url/admin/meta-box.php 文件中的 fifu_save_dimensions 方法,替换为以下内空:
function fifu_save_dimensions($att_id, $width, $height, $url) {
if (!$att_id || !$width || !$height)
return;
$metadata = null;
$metadata['width'] = $width;
$metadata['height'] = $height;
$metadata['file'] = $url;
// 以下为新添加内容
$substring = strrchr($url, "/"); // 查找最后一个点的位置,并包括该点及其后面的字符
$attachment_prex = strrchr($substring, ".");
$attachment_name = mb_substr($substring, 1, 4); // 从第3个字符开始截取
$post_thumbnail = null;
$post_thumbnail['file'] = $attachment_name.'.th'.$attachment_prex;
$post_thumbnail['width'] = 280;
$post_thumbnail['height'] = 210;
$post_thumbnail['mime-type'] = 'image/jpeg';
$post_featured_small_thumb = null;
$post_featured_small_thumb['file'] = $attachment_name.'.md'.$attachment_prex;
$post_featured_small_thumb['width'] = 440;
$post_featured_small_thumb['height'] = 220;
$post_featured_small_thumb['mime-type'] = 'image/jpeg';
$post_thumb = null;
$post_thumb['file'] = $attachment_name.'.th'.$attachment_prex;
$post_thumb['width'] = 280;
$post_thumb['height'] = 210;
$post_thumb['mime-type'] = 'image/jpeg';
$widget_thumb = null;
$widget_thumb['file'] = $attachment_name.'.md'.$attachment_prex;
$widget_thumb['width'] = 300;
$widget_thumb['height'] = 150;
$widget_thumb['mime-type'] = 'image/jpeg';
$sizes = null;
$sizes['post-thumbnail'] = $post_thumbnail;
$sizes['post-blogsite_featured_small_thumb'] = $post_featured_small_thumb;
$sizes['post-blogsite_post_thumb'] = $post_thumb;
$sizes['blogsite_widget_thumb'] = $widget_thumb;
$metadata['sizes'] = $sizes;
wp_update_attachment_metadata($att_id, $metadata);
}2. 修改 db.php 文件
修改 featured-image-from-url/admin/meta-box.php 文件中的 insert_attachment_by 方法,添加以下内空:
$value = str_replace('77777', '1', $value);完整内容如下:
function insert_attachment_by($value) {
$value = str_replace('77777', '1', $value);
$this->wpdb->query("
INSERT INTO {$this->posts} (post_author, guid, post_title, post_mime_type, post_type, post_status, post_parent, post_date, post_date_gmt, post_modified, post_modified_gmt, post_content, post_excerpt, to_ping, pinged, post_content_filtered)
VALUES " . str_replace('\\', '', $value));
}
结束语
使用 Featured Image from URL (FIFU) 插件可以很方便地将外链图片设置为特色图片,本文简单分享了一下使用 Blogsite 主题时,使用 Featured Image from URL (FIFU) 插件需要优化的几处内容,供有此需求的朋友参考。如果有任何意见或建议,欢迎在下方评论处留言。
本文已通过「原本」原创作品认证,转载请注明文章出处及链接。




1F
省去了上传本地的空间占用。