如何移除除WooCommerce主页面外所有后台页面的管理员通知?
移除WooCommerce后台以外页面的管理员通知
方法一:PHP钩子移除(推荐)
通过WordPress后台初始化钩子判断当前页面是否为WooCommerce主页面,若非则移除所有管理员通知的回调函数,彻底屏蔽通知。
将以下代码添加到当前主题的functions.php文件,或自定义插件中:
add_action('admin_init', 'remove_non_woocommerce_admin_notices'); function remove_non_woocommerce_admin_notices() { $current_screen = get_current_screen(); // 验证当前页面是否为WooCommerce主后台页面 if (!$current_screen || $current_screen->id !== 'woocommerce_page_wc-admin') { // 移除所有管理员通知相关钩子 remove_all_actions('admin_notices'); remove_all_actions('all_admin_notices'); } }
注意事项:
- 若你的WooCommerce主页面屏幕ID不同,可查看页面源码中
body标签的screen-xxx类获取准确ID(例如旧版WooCommerce可能为screen-woocommerce)。 - 若需保留特定通知(如WordPress核心更新提示),可修改代码,仅移除指定插件/主题的通知钩子,而非全部移除。
方法二:CSS隐藏通知
如果不想修改PHP代码,可通过CSS隐藏非WooCommerce页面的通知,实现视觉屏蔽:
将以下代码添加到主题的自定义CSS面板,或通过PHP钩子输出:
add_action('admin_head', 'hide_non_woocommerce_admin_notices_css'); function hide_non_woocommerce_admin_notices_css() { $current_screen = get_current_screen(); if (!$current_screen || $current_screen->id !== 'woocommerce_page_wc-admin') { echo '<style> #wpbody-content .notice, #wpbody-content .error, #wpbody-content .updated { display: none !important; } </style>'; } }
内容的提问来源于stack exchange,提问作者Døner M.
相关产品推荐
相关产品推荐

