Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created November 28, 2012 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boonebgorges/4165099 to your computer and use it in GitHub Desktop.
Save boonebgorges/4165099 to your computer and use it in GitHub Desktop.
A function for reliably testing whether you're in the WordPress Dashboard
<?php
/**
* Are we looking at the WordPress admin?
*
* Because AJAX requests are sent to wp-admin/admin-ajax.php, WordPress's
* is_admin() function returns true when DOING_AJAX. This function contains
* logic to test whether AJAX requests are coming from the front end or from
* the Dashboard.
*
* @return bool
*/
function bbg_is_admin() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
// wp_get_referer() should contain the URL of the requesting
// page. Check to see whether it's an admin page
$is_admin = 0 === strpos( wp_get_referer(), admin_url() );
} else {
$is_admin = is_admin();
}
return $is_admin;
}
@espellcaste
Copy link

Clever!

Could you exemplify a use case for this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment