Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect "is-gutenberg" via PHP and JS #12200

Closed
elliotcondon opened this issue Nov 22, 2018 · 12 comments
Closed

How to detect "is-gutenberg" via PHP and JS #12200

elliotcondon opened this issue Nov 22, 2018 · 12 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@elliotcondon
Copy link

elliotcondon commented Nov 22, 2018

What is the easiest way to detect if the current edit screen is using Gutenberg in both PHP and JS.

For example, in PHP:

add_action('init', 'my_init');

function my_init() {
	
	// Detect if is Gutenberg.
	if( is_gutenberg_editor_active() ) {
		// Do something.
	} else {
		// Do something else.
	}
}

And in JS:

$(document).ready(function(){
	
	// Detect if is Gutenberg.
	if( wp.isGutenbergActive() ) {
		// Do something.
	} else {
		// Do something else.
	}
	 
 });

⚠️ Please keep all replies on topic. This should be a simple and straightforward question to answer without philosophical misdirection about "Classic vs Gutenberg" or "React vs PHP".

Thanks
Elliot

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. labels Nov 23, 2018
@rilwis
Copy link

rilwis commented Nov 29, 2018

For JS, I'm using:

function isGutenbergActive() {
    return typeof wp !== 'undefined' && typeof wp.blocks !== 'undefined';
}

@designsimply
Copy link
Member

@elliotcondon I found some sample code that I think will help you with the PHP part of your question at https://wordpress.stackexchange.com/a/309955/10377. Can you have a look?

@elliotcondon
Copy link
Author

@rilwis Good idea. Be sure to test with YoastSEO + Classic Editor. I ran into some issues where wp.blocks was defined when editing classic.

@designsimply Thanks. get_current_screen()->is_block_editor() looks to be the best PHP option.

@designsimply
Copy link
Member

Ace! Closing as it looks like everything's covered now.

@elliotcondon
Copy link
Author

@designsimply I was hoping to get an official response from the gutenberg developers as to how we should detect "is-gutenberg" in JS.

By closing the ticket are you confirming the above solution is the official solution?

@designsimply
Copy link
Member

By closing the ticket are you confirming the above solution is the official solution?

No. It’s just a solution! Help requests can and should be answered by any contributor.

I think to get an official response on something like this, you will want to submit what you’ve found as a documentation pull request to ask for a code review and that would be really cool because it will have the extra benefit of helping others in the community. Would you open a PR for this?

@elliotcondon
Copy link
Author

@designsimply Thanks for the reply. I'm hesitant to open a PR regarding a question. Perhaps I am anticipating incorrectly, but I don't see this going down well.

@noisysocks Quick question. If I as a plugin developer have a question about best practice or "how can I do this with PHP/JS?", where should I ask this question? Here on GitHub, or should I submit a PR containing an edited ".md" file with a question in it?

@noisysocks
Copy link
Member

I think that we're trying to move help requests out of GitHub to somewhere more suitable e.g. the WordPress support forums. In your case I'd probably plop quick questions into #core-editor on WP Slack.

Regarding this question: I recommend detecting something more specific than "is Gutenberg loaded on the page?" because the line between Gutenberg and Core will become blurrier as we gradually use Gutenberg code on more and more WP Admin pages.

For detecting if we're editing a post or page using the block editor, I would go with:

  • get_current_screen()->is_block_editor() in PHP
  • document.body.classList.contains( 'block-editor-page' ) in JavaScript

@elliotcondon
Copy link
Author

@noisysocks Thanks for the reply. IMO I'm not sure we should be asking these kinds of questions on slack. Having the question and answer available via google search (stackoverflow / github / etc) will help multiple developers who also have the same question.

@noisysocks
Copy link
Member

That's a very good point.

@elliotcondon
Copy link
Author

@noisysocks That's why they pay me the big bucks 😉.

@aduth
Copy link
Member

aduth commented Feb 1, 2019

Worth noting: get_current_screen()->is_block_editor() is the function one should use, and will work as expected with WordPress 5.x, but currently wrongly returns false when using the block editor while the Gutenberg plugin is active. This is already fixed in #13449, slated for release with Gutenberg 5.0.

In the meantime, you may consider checking for the presence of a defined is_gutenberg_page function and using it as a fallback (example). This should be considered a temporary measure, as Gutenberg will start deprecating these functions in favor of the core alternatives (#13569).

talonon pushed a commit to kps3/wordpress-localization-plugin that referenced this issue Jul 31, 2019
…lsely detected as enabled when the Classic Editor Plugin is installed and Yoast SEO is installed. Yoast SEO enqueues the React library, which causes false positive on Gutenberg enabled. This false positive causes javascript errors when adding a page to a translation job or adding a page to an existing job.

Using body class detection method defined WordPress/gutenberg#12200 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

5 participants