WordPress插件 侧边栏用户封面图和缩略图 使用OSS自定义规则 【AI编写的】-zibll教程分享社区-zibll子比主题-WordPress主题模板-zibll子比主题

WordPress插件 侧边栏用户封面图和缩略图 使用OSS自定义规则 【AI编写的】

搞这个插件的理由

主要问题出现在主题的用户封面,侧边栏显示用户信息的时候,发现这个封面图怎么都是调用原图,虽说图片上传的时候我设置好了压缩,但还是有个人上传了一张大的图片,我压缩之后依旧有1MB大小,所以就搞出这个插件了。

用户封面图我设置的是除开在用户中心页面显示原图,其余页面都显示缩略图。

文章缩略图图片链接判断如果是自己OSS域名就调用OSS设置的缩略图样式,用户封面图逻辑也是如此。

所以开启和关闭插件都不会影响你很早之前发表的文章图片。

代码

自己新建插件文件夹,文件夹新建PHP文件,最后把代码复制进去就行了

<?php
/*
Plugin Name: Custom Author and Thumbnail Plugin
Description: 允许用户在后台自定义作者封面图链接后缀和文章缩略图处理样式。
Version: 1.0
Author: ღ星晴
*/

// 创建设置页面
add_action('admin_menu', 'custom_author_thumbnail_plugin_menu');
function custom_author_thumbnail_plugin_menu() {
    add_options_page('Custom Author and Thumbnail Settings', '子比图片处理', 'manage_options', 'custom-author-thumbnail-plugin', 'custom_author_thumbnail_plugin_options');
}

// 显示设置页面
function custom_author_thumbnail_plugin_options() {
    ?>
    <div class="wrap">
        <h1>封面图-缩略图自定义设置</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('custom_author_thumbnail_plugin_options_group');
            do_settings_sections('custom-author-thumbnail-plugin');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

// 注册设置
add_action('admin_init', 'custom_author_thumbnail_plugin_settings');
function custom_author_thumbnail_plugin_settings() {
    register_setting('custom_author_thumbnail_plugin_options_group', 'custom_author_cover_suffix');
    register_setting('custom_author_thumbnail_plugin_options_group', 'custom_thumbnail_domain');
    register_setting('custom_author_thumbnail_plugin_options_group', 'custom_thumbnail_code');
    add_settings_section('custom_author_thumbnail_plugin_main_section', '设置', null, 'custom-author-thumbnail-plugin');
    add_settings_field('custom_thumbnail_domain', 'OSS域名', 'custom_thumbnail_domain_callback', 'custom-author-thumbnail-plugin', 'custom_author_thumbnail_plugin_main_section');
    add_settings_field('custom_author_cover_suffix', '封面图图像处理代码', 'custom_author_cover_suffix_callback', 'custom-author-thumbnail-plugin', 'custom_author_thumbnail_plugin_main_section');
    add_settings_field('custom_thumbnail_code', '缩略图图像处理代码', 'custom_thumbnail_code_callback', 'custom-author-thumbnail-plugin', 'custom_author_thumbnail_plugin_main_section');
}

function custom_author_cover_suffix_callback() {
    $suffix = get_option('custom_author_cover_suffix', '!style:thumbnail');
    echo '<input type="text" id="custom_author_cover_suffix" name="custom_author_cover_suffix" value="' . esc_attr($suffix) . '" />';
}

function custom_thumbnail_domain_callback() {
    $domain = get_option('custom_thumbnail_domain', '');
    echo '<input type="text" id="custom_thumbnail_domain" name="custom_thumbnail_domain" value="' . esc_attr($domain) . '" />';
}

function custom_thumbnail_code_callback() {
    $code = get_option('custom_thumbnail_code', '');
    echo '<input type="text" id="custom_thumbnail_code" name="custom_thumbnail_code" value="' . esc_attr($code) . '" />';
}

// 修改封面图片链接
function custom_get_user_cover_img_url($user_id) {
    $img         = $user_id ? zib_get_user_meta($user_id, 'cover_image', true) : '';
    $default_img = _pz('user_cover_img', ZIB_TEMPLATE_DIRECTORY_URI . '/img/user_t.jpg');
    $cover_img_url     = $img ? $img : $default_img;

    // 获取用户自定义的后缀
    $suffix = get_option('custom_author_cover_suffix', '!style:thumbnail');
    $domain = get_option('custom_thumbnail_domain', '');
    // 检查当前页面的 URL
    if ($domain && $suffix && strpos($cover_img_url, $domain) !== false && $_SERVER['REQUEST_URI'] !== '/user/') {
        $cover_img_url .= $suffix;
    }

    return $cover_img_url;
}

// 修改缩略图链接
function custom_modify_thumbnail_url($img_url) {
    $domain = get_option('custom_thumbnail_domain', '');
    $code = get_option('custom_thumbnail_code', '');
    if ($domain && $code && strpos($img_url, $domain) !== false) {
        $img_url .= $code;
    }
    return $img_url;
}

// 替换主题中的函数
function replace_theme_function()
{
    $theme_file = get_template_directory() . '/inc/functions/zib-theme.php';
    $theme_code = file_get_contents($theme_file);

    // 替换 get_user_cover_img_url 调用
    $search  = "\$url = get_user_cover_img_url(\$user_id);";
    $replace = "\$url = custom_get_user_cover_img_url(\$user_id);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    // 替换 zib_post_thumbnail 函数中的缩略图链接
    $search  = "return sprintf('<img' . \$r_attr . ' src=\"%s\" data-src=\"%s\" alt=\"%s\" class=\"lazyload ' . \$class . '\">', \$lazy_thumb, \$img_url, \$alt);";
    $replace = "return sprintf('<img' . \$r_attr . ' src=\"%s\" data-src=\"%s\" alt=\"%s\" class=\"lazyload ' . \$class . '\">', \$lazy_thumb, custom_modify_thumbnail_url(\$img_url), \$alt);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    $search  = "return sprintf('<img' . \$r_attr . ' src=\"%s\" alt=\"%s\" class=\"' . \$class . '\">', \$img_url, \$alt);";
    $replace = "return sprintf('<img' . \$r_attr . ' src=\"%s\" alt=\"%s\" class=\"' . \$class . '\">', custom_modify_thumbnail_url(\$img_url), \$alt);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    file_put_contents($theme_file, $theme_code);
}

function restore_theme_function()
{
    $theme_file = get_template_directory() . '/inc/functions/zib-theme.php';
    $theme_code = file_get_contents($theme_file);

    // 恢复 get_user_cover_img_url 调用
    $search  = "\$url = custom_get_user_cover_img_url(\$user_id);";
    $replace = "\$url = get_user_cover_img_url(\$user_id);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    // 恢复 zib_post_thumbnail 函数中的缩略图链接
    $search  = "return sprintf('<img' . \$r_attr . ' src=\"%s\" data-src=\"%s\" alt=\"%s\" class=\"lazyload ' . \$class . '\">', \$lazy_thumb, custom_modify_thumbnail_url(\$img_url), \$alt);";
    $replace = "return sprintf('<img' . \$r_attr . ' src=\"%s\" data-src=\"%s\" alt=\"%s\" class=\"lazyload ' . \$class . '\">', \$lazy_thumb, \$img_url, \$alt);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    $search  = "return sprintf('<img' . \$r_attr . ' src=\"%s\" alt=\"%s\" class=\"' . \$class . '\">', custom_modify_thumbnail_url(\$img_url), \$alt);";
    $replace = "return sprintf('<img' . \$r_attr . ' src=\"%s\" alt=\"%s\" class=\"' . \$class . '\">', \$img_url, \$alt);";
    if (strpos($theme_code, $search) !== false) {
        $theme_code = str_replace($search, $replace, $theme_code);
    }

    file_put_contents($theme_file, $theme_code);
}

// 刷新 OPCache 缓存
function refresh_opcache() {
    if (function_exists('opcache_reset')) {
        opcache_reset();
    }
}

// 刷新 Redis 缓存
function refresh_redis_cache() {
    if (class_exists('Redis')) {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379); // 根据你的 Redis 配置进行调整
        $redis->flushAll();
    }
}
// 在插件启用时刷新缓存
register_activation_hook(__FILE__, 'replace_theme_function');
register_activation_hook(__FILE__, 'refresh_opcache');
register_activation_hook(__FILE__, 'refresh_redis_cache');

// 在插件禁用时恢复缓存
register_deactivation_hook(__FILE__, 'restore_theme_function');
register_deactivation_hook(__FILE__, 'refresh_opcache');
register_deactivation_hook(__FILE__, 'refresh_redis_cache');

预览图

20250128133703666-image-107

20250128134124959-image-108

插件启用的时候会修改主题代码,禁用的时候会恢复主题代码,主题更新,只要对应的代码没变化,后台禁用插件再启用插件,效果应该是有的。

虽说一直知道修改主题代码是非常不推荐的,但是自己用的是小厂(缤纷云 Bitiful – 强大低成本的对象存储和CDN服务),提供的免费额度小站长几乎不用担心不够用,但是体量小,几乎没插件能完美支持,接替WordPress媒体库但是没有图片处理,有图片处理的插件却不支接替媒体库,趁着过年的时候想着用AI辅助帮忙搞一下,就捣鼓出这个插件了,反正将就着用,小厂源自于大厂,所以其他大厂的OSS几乎是通用的。

该插件不提供接替媒体库,OSS插件还是需要自己去找对应的,有位大佬做了一个几乎全能的S3(OSS)插件,但是价格挺贵,就看大家接受不接受了。

插件地址:支持 S3 的 WordPress 和 Typecho 云存储插件 – 沈唁志

20250128135707515-image-109

插件作者因为懒得去兼容各大主题的缩略图,所以压根没做,只有接替媒体库的功能。

效果

主要还是担心流量上,首页你看着缩略图挺小的,其实还是下载原图,多刷新几次就会发现流量跑的飞快。

禁用状态下的资源使用情况

20250128141255354-image-113

开启状态下的资源使用情况

20250128140938284-image-112

效果还是立竿见影的,插件开启禁用的时候都会刷新opcache和Redis缓存,所以点击开启和禁用的时候会感觉卡卡的。

压缩包下载

请登录后发表评论