钩子文档

admin_init

💡 云策文档标注

概述

admin_init 是一个 WordPress 钩子,在管理屏幕或脚本初始化时触发。它类似于 init 钩子,但专门用于管理区域,包括 admin-ajax.php 和 admin-post.php。

关键要点

  • admin_init 钩子在用户访问管理区域时,在其他钩子之前触发。
  • 该钩子不提供任何参数,只能用于回调指定函数。
  • 它适用于所有管理相关屏幕,包括 Ajax 和 POST 请求处理。
  • 常用于注册插件设置、限制管理访问或执行其他管理端初始化任务。

代码示例

// 示例1:注册插件设置
function myplugin_settings() {
    register_setting( 'myplugin', 'myplugin_setting_1', 'intval' );
    register_setting( 'myplugin', 'myplugin_setting_2', 'intval' );
}
add_action( 'admin_init', 'myplugin_settings' );

// 示例2:限制非管理员访问管理屏幕(重定向)
function restrict_admin_with_redirect() {
    if ( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
        wp_safe_redirect( site_url() );
        exit;
    }
}
add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );

// 示例3:限制非管理员访问管理屏幕(显示消息)
function restrict_admin() {
    if ( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
        wp_die( __( 'You are not allowed to access this part of the site' ) );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );

// 示例4:在管理屏幕初始化时执行操作
function the_dramatist_fire_on_admin_screen_initialization() {
    echo 'Hello World';
}
add_action( 'admin_init', 'the_dramatist_fire_on_admin_screen_initialization' );

注意事项

  • admin_init 钩子不仅运行在用户可见的管理屏幕,也运行在 admin-ajax.php 和 admin-post.php 脚本中。
  • 使用此钩子时,需注意权限检查,以避免影响 Ajax 请求的正常功能。
  • 钩子优先级可调整,例如示例中使用了优先级 1 来确保早期执行。

📄 原文内容

Fires as an admin screen or script is being initialized.

Description

Note, this does not just run on user-facing admin screens.
It runs on admin-ajax.php and admin-post.php as well.

This is roughly analogous to the more general ‘init’ hook, which fires earlier.

More Information

admin_init is triggered before any other hook when a user accesses the admin area.

This hook doesn’t provide any parameters, so it can only be used to callback a specified function.

Source

do_action( 'admin_init' );

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Example migrated from Codex:

    Another typical usage is to register a new setting for use by a plugin:

    function myplugin_settings() {
        register_setting( 'myplugin', 'myplugin_setting_1', 'intval' );
        register_setting( 'myplugin', 'myplugin_setting_2', 'intval' );
    }
    add_action( 'admin_init', 'myplugin_settings' );

  2. Skip to note 6 content

    Example migrated from Codex:

    This example works similarly to the first example, but it will automatically redirect users lacking the specified capability to the homepage.

    /**
     * Restrict access to the administration screens.
     *
     * Only administrators will be allowed to access the admin screens,
     * all other users will be automatically redirected to the front of
     * the site instead.
     *
     * We do allow access for Ajax requests though, since these may be
     * initiated from the front end of the site by non-admin users.
     */
    function restrict_admin_with_redirect() {
    
    	if ( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
    		wp_safe_redirect( site_url() ); 
    		exit;
    	}
    }
    
    add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );

  3. Skip to note 7 content

    Example migrated from Codex:

    In this example, we block access to the admin panel for users that do not have the Administrator Role.

    /**
     * Restrict access to the administration screens.
     *
     * Only administrators will be allowed to access the admin screens,
     * all other users will be shown a message instead.
     *
     * We do allow access for Ajax requests though, since these may be
     * initiated from the front end of the site by non-admin users.
     */
    function restrict_admin() {
    
    	if ( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
    		wp_die( __( 'You are not allowed to access this part of the site' ) );
    	}
    }
    add_action( 'admin_init', 'restrict_admin', 1 );

  4. Skip to note 8 content

    /**
     * Fire on the initialization of the admin screen or scripts.
     */
    function the_dramatist_fire_on_admin_screen_initialization() {
        // Do stuff. Say we will echo "Hello World".
        echo 'Hello World';
    }
    add_action( 'admin_init', 'the_dramatist_fire_on_admin_screen_initialization' );

    The above piece of code will only echo “Hello World” if the user is in admin screen.