当前浏览:首页 / WordPress钩子 / admin_notices

do_action( 'admin_notices' )

打印后台管理屏幕通知

adminmore...


更多信息

实例

为了显示通知,使用notice样式类和以下类之一回显div:

*notice-error–将以白色背景和red左边框显示消息。
*notice-warning–将以白色背景和yellow/orange左边框显示消息。
*notice-success–将以白色背景和green左边框显示消息。
*notice-info–将以白色背景和blue左边框显示消息。
*可以选择使用is-dismissible通过JavaScript在消息中添加关闭图标。然而,它的行为仅适用于当前屏幕。一旦重新加载页面或加载另一个页面,它不会阻止消息再次出现。

不要使用update-nag作为类名!
它不适用于常规的管理通知,因为它将对消息应用不同的布局样式。此外,它将触发消息移动到页面标题上方(<h1>)从而在语义上将其置于其他通知之上,这在插件或主题环境中可能并不合适。

div的内部内容是消息,最好将消息包装在段落标记<p>中,以获得输出中正确的填充量。

function sample_admin_notice__success() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );
function sample_admin_notice__error() {
	$class = 'notice notice-error';
	$message = __( 'Irks! An error has occurred.', 'sample-text-domain' );

	printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}
add_action( 'admin_notices', 'sample_admin_notice__error' );

源码

查看源码 官方文档


更新日志

版本描述
3.1.0开始引入

用户贡献的笔记

  • 贡献者:Patrick JohannesonBart Kuijper

    示例用法

    function sample_admin_notice__success() {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
        </div>
        <?php
    }
    add_action( 'admin_notices', 'sample_admin_notice__success' );

    notice-success类将以白色背景和绿色左边框显示消息。

    function sample_admin_notice__error() {
    	$class = 'notice notice-error';
    	$message = __( 'Irks! An error has occurred.', 'sample-text-domain' );
    
    	printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 
    }
    add_action( 'admin_notices', 'sample_admin_notice__error' );

    notice-error将以白色背景和红色左边框显示消息。

    使用notice-warning表示yellow/orange,使用notice-info表示蓝色左边框。

    类名is-dismissible将自动触发一个关闭图标,通过JavaScript将其添加到消息中。然而,它的行为仅适用于当前屏幕。一旦重新加载页面或加载另一个页面,它不会阻止消息再次出现。

  • 贡献者:Amaan Khan

    动态显示消息的方法(以警告消息为例)

    /**
     * Class to log warnings.
     */
    class Log_Warning {
    	/**
    	 * Message to be displayed in a warning.
    	 *
    	 * @var string
    	 */
    	private string $message;
    
    	/**
    	 * Initialize class.
    	 *
    	 * @param string $message Message to be displayed in a warning.
    	 */
    	public function __construct( string $message ) {
    		$this->message = $message;
    
    		add_action( 'admin_notices', array( $this, 'render' ) );
    	}
    
    	/**
    	 * Displays warning on the admin screen.
    	 *
    	 * @return void
    	 */
    	public function render() {
    		printf( '<div class="notice notice-warning is-dismissible"><p>Warning: %s</p></div>', esc_html( $this->message ) );
    	}
    }

    现在,您可以简单地初始化类以显示动态消息。

    new Log_Warning( 'Image should contain async tag' );
  • 贡献者:Benny

    主题审查团队(TRT)发布了一个包,您可以在主题或插件中使用它来创建管理通知:
    7912rhttps://github.com/WPTrT/admin-notices

    它的主要目的是提供一种使用默认WordPress样式以一致方式创建管理通知的标准化方法。

    使用此方法创建的通知将自动取消。

  • 贡献者:Philipp Stracker

    关于非官方的、自愿的标志'DISABLE_NAG_NOTICES'的信息缺失。以下是旧codex页的相关详细信息:

    禁用Nag通知
    2017年末,LittleBizzy提出了一个非官方定义的常量,作为插件或主题作者的一种自愿方式,以允许隐藏某些管理员通知,这些通知可能会被一些网站管理员视为麻烦。其用途如下:

    define('DISABLE_NAG_NOTICES', true);

    需要注意的是,这个常量并没有得到普遍支持,尽管已经有少数插件和主题作者开始支持它。一个典型的用例可能是隐藏重复出现的“nag”通知,要求用户在WordPress.org上查看其扩展等。此外,它不应对WP Admin中出现的一般通知产生任何影响,只是扩展人员自行选择禁用某些通知的一种方式。

    使用示例:

    if ( ! defined( 'DISABLE_NAG_NOTICES' ) || ! DISABLE_NAG_NOTICES ) {
    	add_action( 'admin_notices', 'my_admin_notice_renderer' );
    }
  • 贡献者:Chigozie Orunta

    这里有一个简单的方法,可以在一些管理页面中添加独立日通知。

    add_action( 'admin_notices', 'independence_notice' );
    
    function independence_notice() {
        global $pagenow;
    	$admin_pages = [ 'index.php', 'edit.php', 'plugins.php' ];
        if ( in_array( $pagenow, $admin_pages ) ) {
    		if ( date( 'j, F' ) === '1, October' ) { 
    			?>
    			<div class="notice notice-warning is-dismissible">
    				<p>Happy Independence Day, Nigeria...</p>
    			</div>
    			<?
    		}
        }
    }
  • 贡献者:Munir Mahmud
    //Display admin notices 
    function my_test_plugin_admin_notice()
    {
    	//get the current screen
    	$screen = get_current_screen();
    
    	//return if not plugin settings page 
    	//To get the exact your screen ID just do var_dump($screen)
    	if ( $screen->id !== 'toplevel_page_YOUR_PLUGIN_PAGE_SLUG') return;
    		
    	//Checks if settings updated 
    	if ( isset( $_GET['settings-updated'] ) ) {
    		//if settings updated successfully 
    		if ( 'true' === $_GET['settings-updated'] ) : ?>
    
    			<div class="notice notice-success is-dismissible">
    				<p><?php _e('Congratulations! You did a very good job.', 'textdomain') ?></p>
    			</div>
    
    		<?php else : ?>
    
    			<div class="notice notice-warning is-dismissible">
    				<p><?php _e('Sorry, I can not go through this.', 'textdomain') ?></p>
    			</div>
    			
    		<?php endif;
    	}
    }
    add_action( 'admin_notices', 'my_test_plugin_admin_notice' );

    notice-success类将以白色背景和绿色左边框显示消息,notice-error类将以白色底和红色左边框显示信息。

    如果要显示错误,还可以使用notice-warning蓝色左边框的yellow/orange的警告。