Skip to content

Instantly share code, notes, and snippets.

@maddisondesigns
Last active September 11, 2023 07:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maddisondesigns/765cb301312c87f9818766735764a948 to your computer and use it in GitHub Desktop.
Save maddisondesigns/765cb301312c87f9818766735764a948 to your computer and use it in GitHub Desktop.
Better WordPress is_admin()
<?php
/**
* Check if inside WP Admin. Also works in the Block Editor.
*
* With the introduction of Gutenberg, is_admin() was broken.
* This better version will account for the Block Editor (Gutenberg)
*/
function mytheme_better_is_admin() {
// Check if in Block Editor - see: https://github.com/WordPress/gutenberg/issues/51090#issuecomment-1576570247
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === $_GET['context'] ) ) {
return true;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment