Last active
March 6, 2019 17:29
-
-
Save ryanemitchell/5896e5a2525fd3b4c5cb1239d955653b to your computer and use it in GitHub Desktop.
For detecting if a the PHP used in a ServerSideRender component is loaded in the frontend or in the editor. Checks the URL serving the file for block-renderer that only appears when loaded in the editor. Useful for conditional display of elements in the php when components are used for these elements in the editor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_frontend_context() { | |
// Check if page is being loaded in the editor by checking URL structure of the ServerSideRender source for block-renderer | |
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | |
if (strpos($url,'block-renderer') !== false) { | |
// In wp-admin | |
return false; | |
} else { | |
// front end | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment