Skip to content

Instantly share code, notes, and snippets.

@fklein-lu
Last active October 7, 2020 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fklein-lu/581a83b1baebcabeaf6d53908ebca6ab to your computer and use it in GitHub Desktop.
Save fklein-lu/581a83b1baebcabeaf6d53908ebca6ab to your computer and use it in GitHub Desktop.
Gutenberg block development helper function to detect whether a block renders on the frontend or the backend
<?php
/**
* Hackish solution to distinguish between block renders on the front end, and the back end.
*
* In the admin, the global `$wp_query` does not contain a query. We use this to determine whether a rendering is
* happening on the front end or the back end.
*
* @return bool Whether we're in the admin or on the front end.
*/
function fk_is_front_end_query() {
return ! is_null( $GLOBALS['wp_query']->query );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment