目次
更新の通知を表示したくない
WordPressの更新自体は当然やらないといけないのですが、管理者以外の人がログインしている時にも表示されるため、「更新しなくていいの?」みたいに言われるわけで。そこで管理者以外は非表示にするように制御したかったのでメモ。

remove_action()関数でフックポイントを削除
remove_action
関数を使ってお知らせフックポイントadmin_notices
を指定してupdate_nag
関数をコールバック関数に指定します。
function hide_update_notice() {
remove_action( 'admin_notices', 'update_nag' );
}
add_action( 'admin_init', 'hide_update_notice' );
管理者権限の時だけは表示したい
当初の目的は管理者権限以外では非表示にしたかったので、ログインしている権限で分岐して削除する処理を追加します。current_user_can
関数で分岐。
function hide_update_notice() {
if ( !current_user_can( 'administrator' ) ) {
remove_action( 'admin_notices', 'update_nag' );
}
}
add_action( 'admin_init', 'hide_update_notice' );
できた!\(^o^)/
これで完成。ユーザーのcapability(権限)の文字列を忘れがちなので以下にメモ残し。
管理者 | administrator |
---|---|
編集者 | editor |
投稿者 | author |
寄稿者 | contributor |
購読者 | subscriber |
参考サイト
- Developer Resources: update_nag()
- https://developer.wordpress.org/reference/functions/update_nag/
- Developer Resources: remove_action()
- https://developer.wordpress.org/reference/functions/remove_action/
- Developer Resources: current_user_can()
- https://developer.wordpress.org/reference/functions/current_user_can/