禁用插件更新示例

/**
 * 禁用所有插件自动更新并隐藏插件更新通知
 * 放到: wp-content/mu-plugins/disable-all-plugin-updates.php
 *
 * 说明:
 * - 适用于 single-site 和 multisite(同时处理 site_transient / transient)。
 * - 把文件放在 mu-plugins 根目录(不是子目录),WP 会在每次加载时包含它。
 * - 部署后请清除 transient / object cache(见下面的步骤)。
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/* 1) 禁用所有插件的自动更新(只影响自动更新行为) */
add_filter( 'auto_update_plugin', '__return_false' );

/* 2) 在读取更新的 transient 之前短路,返回“无更新” —— single + multisite */
add_filter( 'pre_transient_update_plugins', '__return_null' );
add_filter( 'pre_site_transient_update_plugins', '__return_null' );

/* 3) 作为后备:确保如果 transient 被加载,清空 response,使 WP 看不到任何插件更新 */
$dpwu_empty_response = function( $transient ) {
    if ( empty( $transient ) ) {
        return $transient;
    }

    // transient->response 在 WP 中通常是数组,确保它为空数组
    if ( isset( $transient->response ) && is_array( $transient->response ) ) {
        $transient->response = array();
    }

    // 兼容不同 WP 版本,尽量移除任何记录更新的字段
    if ( isset( $transient->updates ) ) {
        $transient->updates = array();
    }

    return $transient;
};

add_filter( 'transient_update_plugins', $dpwu_empty_response );
add_filter( 'site_transient_update_plugins', $dpwu_empty_response );

/* 4) 辅助函数:清除与更新相关的 transient 与 object cache(手动运行或在需要时启用自动清理) */
if ( ! function_exists( 'dpwu_clear_update_cache' ) ) {
    function dpwu_clear_update_cache() {
        // 单站点 transient
        if ( function_exists( 'delete_transient' ) ) {
            @delete_transient( 'update_plugins' );
        }
        // 多站点 site_transient
        if ( function_exists( 'delete_site_transient' ) ) {
            @delete_site_transient( 'update_plugins' );
        }
        // 如果使用对象缓存(Redis/Memcached),尝试清空 WP cache(视环境而定)
        if ( function_exists( 'wp_cache_flush' ) ) {
            @wp_cache_flush();
        }
    }
}

/* 可选:如果你希望每次管理员访问时自动清一次缓存(部署后可注释掉) */
/*
add_action( 'admin_init', function() {
    if ( current_user_can( 'manage_options' ) ) {
        dpwu_clear_update_cache();
    }
}, 1 );
*/

 

阅读剩余
THE END