do_action( 'admin_init' )

Fires as an admin screen or script is being initialized.


Description 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.


Top ↑

More Information 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.


Top ↑

Source Source

File: wp-admin/admin.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by stevenlinx

    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 );
    
  2. Skip to note 2 content
    Contributed by stevenlinx

    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 3 content
    Contributed by stevenlinx

    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' );
    
  4. Skip to note 4 content
    Contributed by Rnaby
    
    /**
     * 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.

You must log in before being able to contribute a note or feedback.