Support » Plugin: Gutenberg » Best way to check for Gutenberg during AJAX call

  • Resolved Tijmen Smit

    (@tijmensmit)


    Before a post is saved in Gutenberg a quick AJAX call is made to a third-party API to validate the data, and based on the response createNotice runs to show feedback.

    In the classic editor this was done with a normal notice, the problem is that when the AJAX call runs the get_current_screen is empty, so there’s no way to check for ‘is_block_editor’ ( it works fine outside the AJAX calls ). So I can not determine whether Gutenberg is active and if I should write the error message to the old admin notices API, or structure it so it can be used as a notice in Gutenberg.

    I have spend some time on Google trying to find a solution for this, but this seems to be a difficult problem. I tried to run set_current_screen as well, but that made no difference.

    The only way I can think of at the moment to see if Gutenberg is active when an AJAX calls runs is a bit of hack, and maybe it’s a horrible idea in the first place.

    But I could simply check the current version of WP and try to make a call to the rest API for the used custom post type. If it’s WP 5 > and the rest API call works, then it should be Gutenberg ( right? ), but this feels like a very hackish workaround that shouldn’t be necessary.

    Is there even a simple way to make get_current_screen work when the AJAX call runs, or is this really a complicated problem?

Viewing 1 replies (of 1 total)
  • Plugin Author Jorge Costa

    (@jorgefilipecosta)

    Hi @tijmensmit!

    A possible simple solution I can think of to solve the use-case would be to on the JavaScript code that runs on Gutenberg pass a flag in the ajax request saying blockEditor=true. On the code that handles the request, one would simply check for the presence of that flag.

    I hope this suggestion is useful, feel free to ask any other questions!

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.