Skip to content

Instantly share code, notes, and snippets.

@kpirnie
Last active August 13, 2025 18:20
Show Gist options
  • Save kpirnie/5716483a8d61628d594781f822c74e7a to your computer and use it in GitHub Desktop.
Save kpirnie/5716483a8d61628d594781f822c74e7a to your computer and use it in GitHub Desktop.
WordPress - Are We Really in Admin?
/**
* is_in_admin
*
* Check and see if we are indeed in the admin area of the site
*
* @since 7.4
* @access public
* @static
* @author Kevin Pirnie <[email protected]>
* @package Kevin's Framework
*
* @return bool Returns if we are or not
*
*/
public static function is_in_admin( ) : bool {
// get the current URL
$_current_url = home_url( add_query_arg( null, null ) );
// get the admin URL
$_admin_url = strtolower( admin_url( ) );
// get the referrer
$_referrer = strtolower( wp_get_referer( ) );
// let's see if it's a block update request
$_block_update_request = self::is_rest_request( ) && strpos( $_admin_url, '/wp-admin/' ) > 0 && ! strpos( $_admin_url, '/wp-admin/admin-ajax.php' );
// check it out
if( $_block_update_request ) {
// it is, return true
return true;
}
// cloud this be an admin request?
if ( 0 === strpos( $_current_url, $_admin_url ) ) {
// did the user come from admin?
if ( 0 === strpos( $_referrer, $_admin_url ) ) {
// they did, return true
return true;
// otherwise
} else {
// is it an admin ajax request?
if( function_exists( 'wp_doing_ajax' ) ) {
// return if we are NOT
return ! wp_doing_ajax( );
// otherwise
} else {
// maybe we actually are?
return ! ( defined( 'DOING_AJAX' ) && DOING_AJAX );
}
}
}
// otherwise just return false
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment