問題描述
我正在嘗試刪除或隱藏 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。

