admin_init

最后更新于:2021-11-25 19:42:25

do_action( ‘admin_init’ )

Fires as an admin screen or script is being initialized.

源文件

文件: gc-admin/admin.php

View on Trac

/**
 * 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' ) && ( ! gc_doing_ajax() ) ) {
		gc_die( __( 'You are not allowed to access this part of the site' ) );
	}
}
add_action( 'admin_init', 'restrict_admin', 1 );

/**
 * 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' ) && ( ! gc_doing_ajax() ) ) {
		gc_safe_redirect( site_url() ); 
		exit;
	}
}

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );