問題描述

我正在嘗試刪除或隱藏 non-admin 使用者的更新 nags 。作為管理員,我看到:

我看到處理這個流行的答案說使用:

function hide_update_nag() {
    if ( !current_user_can('update_core') ) {
        remove_action( 'admin_notices', 'update_nag', 3 );
    }
}
add_action( 'admin_head', 'hide_update_nag', 1 );

這適用於刪除第一個訊息 (WordPress 4.5.3 可用!請現在更新),但離開第二個可見 non-admins:

兩個訊息都包裝在一個<div class="update-nag"> 中,所以 one option 是修改上面的程式碼塊,使用 CSS 來隱藏 nag:

echo '<style>.update-nag {display: none}</style>';

但這對我來說感覺很棒。有沒有辦法掛鉤一個動作或過濾器,並刪除所有的更新訊息為 non-admin 使用者?沒有 third-party 外掛推薦。

最佳解決方案

wp-admin/includes/update.php 檔案

if ( current_user_can('update_core') )
        $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
    else
        $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');

我們可以看到基於當前使用者角色的訊息是不同的,這是 maintenance_nag

基本上我們有兩個更新,可以在 admin-filters.php 中找到

add_action( 'admin_notices', 'update_nag',      3  );
add_action( 'admin_notices', 'maintenance_nag', 10 );

所以要刪除我們可以使用的第二個訊息 (如果你只想要這個 non-admins,也檢查當前的使用者角色)

remove_action( 'admin_notices', 'maintenance_nag', 10 );

對於 multi-site 使用

remove_action( 'network_admin_notices', 'maintenance_nag', 10 );

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。