5

We use JavaScript to unregister some Core blocks from the Page Editor. We'd like to unregister a slightly different list of blocks when using the Site Editor vs when editing a page or post.

Rather than something clunky to make this determination, such as looking at the current page's URL, does WordPress provide any native functions we can rely on? Some JavaScript version of get_current_screen or is_site_editor() or similar?

3
  • Does this answer your question? Gutenberg and FSE: How to exclude a block from full site editing
    – birgire
    Commented Jan 12, 2023 at 9:50
  • That is a super helpful answer and has a lot of important information and ideas to keep in mind. While it didn't directly answer my question, inspecting the page source to look for things mentioned there DID get me exactly what I was looking for. I will post that as an Answer now. thank you! Commented Jan 12, 2023 at 17:31
  • yes, good points made by Tom there regarding possible conflicts removing blocks only from a single context. Otherwise it's also possible to target the wp-edit-site handle with wp_add_inline_script() to add inline script like wp.domReady( function(){ wp.blocks.unregisterBlockType( 'core/html' );}); or use admin_enqueue_scripts for larger scripts. There is also a way to use the PHP filter allowed_block_types_all and target the block editor's context there with $block_editor_context->name === 'core/edit-site' ... etc but we just keep the above in mind.
    – birgire
    Commented Jan 13, 2023 at 10:41

1 Answer 1

1

The Block Editor, in both the context of the editing pages or the Site Editor, actually does provide some simple variables in JavaScript similar to the data that is available via get_current_screen in PHP.

Sample variables in Page Editor:

var ajaxurl = '/mysite/wp-admin/admin-ajax.php',
pagenow = 'page',
typenow = 'page',
adminpage = 'post-new-php',
thousandsSeparator = ',',
decimalPoint = '.',
isRtl = 0;

Sample variables in Site Editor:

var ajaxurl = '/mysite/wp-admin/admin-ajax.php',
pagenow = 'site-editor',
typenow = '',
adminpage = 'site-editor-php',
thousandsSeparator = ',',
decimalPoint = '.',
isRtl = 0;

So one can simply access 'pagenow' to make the determination which context we are in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.