Skip to content

Instantly share code, notes, and snippets.

@ryanemitchell
Last active March 6, 2019 17:29
Show Gist options
  • Save ryanemitchell/5896e5a2525fd3b4c5cb1239d955653b to your computer and use it in GitHub Desktop.
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.
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