How to detect the usage of Gutenberg





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
5
down vote

favorite
2












The new editor called Gutenberg is here as plugin in 4.9, and as core functionality called Block Editor, in 5.0. Regarding to it, it is often needed to determine programmatically which editor is used to edit post or page in the site console. How to do it?










share|improve this question
























  • add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
    – vikrant zilpe
    Nov 30 at 11:06










  • please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
    – vikrant zilpe
    Nov 30 at 11:08










  • and this davidsword.ca/development/…
    – vikrant zilpe
    Nov 30 at 11:09






  • 2




    @vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
    – KAGG Design
    Nov 30 at 11:23












  • ok thanks inform me
    – vikrant zilpe
    Nov 30 at 11:25

















up vote
5
down vote

favorite
2












The new editor called Gutenberg is here as plugin in 4.9, and as core functionality called Block Editor, in 5.0. Regarding to it, it is often needed to determine programmatically which editor is used to edit post or page in the site console. How to do it?










share|improve this question
























  • add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
    – vikrant zilpe
    Nov 30 at 11:06










  • please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
    – vikrant zilpe
    Nov 30 at 11:08










  • and this davidsword.ca/development/…
    – vikrant zilpe
    Nov 30 at 11:09






  • 2




    @vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
    – KAGG Design
    Nov 30 at 11:23












  • ok thanks inform me
    – vikrant zilpe
    Nov 30 at 11:25













up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





The new editor called Gutenberg is here as plugin in 4.9, and as core functionality called Block Editor, in 5.0. Regarding to it, it is often needed to determine programmatically which editor is used to edit post or page in the site console. How to do it?










share|improve this question















The new editor called Gutenberg is here as plugin in 4.9, and as core functionality called Block Editor, in 5.0. Regarding to it, it is often needed to determine programmatically which editor is used to edit post or page in the site console. How to do it?







gutenberg






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 at 11:02









Mark Kaplun

20.2k52754




20.2k52754










asked Nov 30 at 10:01









KAGG Design

1,209211




1,209211












  • add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
    – vikrant zilpe
    Nov 30 at 11:06










  • please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
    – vikrant zilpe
    Nov 30 at 11:08










  • and this davidsword.ca/development/…
    – vikrant zilpe
    Nov 30 at 11:09






  • 2




    @vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
    – KAGG Design
    Nov 30 at 11:23












  • ok thanks inform me
    – vikrant zilpe
    Nov 30 at 11:25


















  • add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
    – vikrant zilpe
    Nov 30 at 11:06










  • please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
    – vikrant zilpe
    Nov 30 at 11:08










  • and this davidsword.ca/development/…
    – vikrant zilpe
    Nov 30 at 11:09






  • 2




    @vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
    – KAGG Design
    Nov 30 at 11:23












  • ok thanks inform me
    – vikrant zilpe
    Nov 30 at 11:25
















add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
– vikrant zilpe
Nov 30 at 11:06




add_action( 'admin_enqueue_scripts', 'wpse_gutenberg_editor_test' ); function wpse_gutenberg_editor_test() { if( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { // your gutenberg editor related CODE here } else { // this is not gutenberg. // this may not even be any editor, you need to check the screen. } }
– vikrant zilpe
Nov 30 at 11:06












please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
– vikrant zilpe
Nov 30 at 11:08




please check this :artiss.blog/2018/09/detecting-per-post-use-of-gutenberg
– vikrant zilpe
Nov 30 at 11:08












and this davidsword.ca/development/…
– vikrant zilpe
Nov 30 at 11:09




and this davidsword.ca/development/…
– vikrant zilpe
Nov 30 at 11:09




2




2




@vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
– KAGG Design
Nov 30 at 11:23






@vikrantzilpe please do not rely to an outdated information. It is better to test what you are writing. For example, the_gutenberg_project() function exists only in Gutenberg plugin, but not in the WP 5.0 Core.
– KAGG Design
Nov 30 at 11:23














ok thanks inform me
– vikrant zilpe
Nov 30 at 11:25




ok thanks inform me
– vikrant zilpe
Nov 30 at 11:25










2 Answers
2






active

oldest

votes

















up vote
7
down vote













There are several variants:




  • WordPress 4.9, Gutenberg plugin is not active

  • WordPress 4.9, Gutenberg plugin is active

  • WordPress 5.0, Block Editor by default

  • WordPress 5.0, Classic Editor plugin is active

  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected


All the mentioned variants can be processed by the following code:



/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function is_active() {
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

if ( ! $gutenberg && ! $block_editor ) {
return false;
}

if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}

if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}

$use_block_editor = get_option( 'classic-editor-replace' ) === 'no-replace';

return $use_block_editor;
}


Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.






share|improve this answer























  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
    – Mark Kaplun
    Nov 30 at 16:02










  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
    – Mark Kaplun
    Nov 30 at 16:05


















up vote
1
down vote













You can use



add_action( 'enqueue_block_editor_assets', 'your_function_name' );


which is only fired when editing content with Gutenberg.






share|improve this answer








New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • This code works only on editor page.
    – KAGG Design
    yesterday










  • What about enqueue_block_assets?
    – Marc
    14 hours ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f320653%2fhow-to-detect-the-usage-of-gutenberg%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote













There are several variants:




  • WordPress 4.9, Gutenberg plugin is not active

  • WordPress 4.9, Gutenberg plugin is active

  • WordPress 5.0, Block Editor by default

  • WordPress 5.0, Classic Editor plugin is active

  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected


All the mentioned variants can be processed by the following code:



/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function is_active() {
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

if ( ! $gutenberg && ! $block_editor ) {
return false;
}

if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}

if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}

$use_block_editor = get_option( 'classic-editor-replace' ) === 'no-replace';

return $use_block_editor;
}


Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.






share|improve this answer























  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
    – Mark Kaplun
    Nov 30 at 16:02










  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
    – Mark Kaplun
    Nov 30 at 16:05















up vote
7
down vote













There are several variants:




  • WordPress 4.9, Gutenberg plugin is not active

  • WordPress 4.9, Gutenberg plugin is active

  • WordPress 5.0, Block Editor by default

  • WordPress 5.0, Classic Editor plugin is active

  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected


All the mentioned variants can be processed by the following code:



/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function is_active() {
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

if ( ! $gutenberg && ! $block_editor ) {
return false;
}

if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}

if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}

$use_block_editor = get_option( 'classic-editor-replace' ) === 'no-replace';

return $use_block_editor;
}


Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.






share|improve this answer























  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
    – Mark Kaplun
    Nov 30 at 16:02










  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
    – Mark Kaplun
    Nov 30 at 16:05













up vote
7
down vote










up vote
7
down vote









There are several variants:




  • WordPress 4.9, Gutenberg plugin is not active

  • WordPress 4.9, Gutenberg plugin is active

  • WordPress 5.0, Block Editor by default

  • WordPress 5.0, Classic Editor plugin is active

  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected


All the mentioned variants can be processed by the following code:



/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function is_active() {
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

if ( ! $gutenberg && ! $block_editor ) {
return false;
}

if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}

if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}

$use_block_editor = get_option( 'classic-editor-replace' ) === 'no-replace';

return $use_block_editor;
}


Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.






share|improve this answer














There are several variants:




  • WordPress 4.9, Gutenberg plugin is not active

  • WordPress 4.9, Gutenberg plugin is active

  • WordPress 5.0, Block Editor by default

  • WordPress 5.0, Classic Editor plugin is active

  • WordPress 5.0, Classic Editor plugin is active, but in site console in “Settings > Writing” the option “Use the Block editor by default…” is selected


All the mentioned variants can be processed by the following code:



/**
* Check if Block Editor is active.
* Must only be used after plugins_loaded action is fired.
*
* @return bool
*/
function is_active() {
// Gutenberg plugin is installed and activated.
$gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );

// Block editor since 5.0.
$block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );

if ( ! $gutenberg && ! $block_editor ) {
return false;
}

if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}

if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
return true;
}

$use_block_editor = get_option( 'classic-editor-replace' ) === 'no-replace';

return $use_block_editor;
}


Function returns true if block editor is active by any means, and false – in the case if classic editor is here. This function must only be used after plugins_loaded action is fired.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 at 14:30









kero

3,02511024




3,02511024










answered Nov 30 at 10:01









KAGG Design

1,209211




1,209211












  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
    – Mark Kaplun
    Nov 30 at 16:02










  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
    – Mark Kaplun
    Nov 30 at 16:05


















  • not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
    – Mark Kaplun
    Nov 30 at 16:02










  • or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
    – Mark Kaplun
    Nov 30 at 16:05
















not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
– Mark Kaplun
Nov 30 at 16:02




not going o downvote, but it is not a fail safe answer. You should do detection based on actions or filters being triggered, not based n the existence of a file or a function. There are several ways to disable gutenberg and the classic editor is only one of them.
– Mark Kaplun
Nov 30 at 16:02












or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
– Mark Kaplun
Nov 30 at 16:05




or to say it differently, you should never even try to detect if gutenberg is used or not, but instead just hook whatever functionality you need on the relevant gutenberg hooks in addition to hooking on the tinymce hooks
– Mark Kaplun
Nov 30 at 16:05












up vote
1
down vote













You can use



add_action( 'enqueue_block_editor_assets', 'your_function_name' );


which is only fired when editing content with Gutenberg.






share|improve this answer








New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • This code works only on editor page.
    – KAGG Design
    yesterday










  • What about enqueue_block_assets?
    – Marc
    14 hours ago















up vote
1
down vote













You can use



add_action( 'enqueue_block_editor_assets', 'your_function_name' );


which is only fired when editing content with Gutenberg.






share|improve this answer








New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • This code works only on editor page.
    – KAGG Design
    yesterday










  • What about enqueue_block_assets?
    – Marc
    14 hours ago













up vote
1
down vote










up vote
1
down vote









You can use



add_action( 'enqueue_block_editor_assets', 'your_function_name' );


which is only fired when editing content with Gutenberg.






share|improve this answer








New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









You can use



add_action( 'enqueue_block_editor_assets', 'your_function_name' );


which is only fired when editing content with Gutenberg.







share|improve this answer








New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Nov 30 at 16:27









Marc

111




111




New contributor




Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Marc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • This code works only on editor page.
    – KAGG Design
    yesterday










  • What about enqueue_block_assets?
    – Marc
    14 hours ago


















  • This code works only on editor page.
    – KAGG Design
    yesterday










  • What about enqueue_block_assets?
    – Marc
    14 hours ago
















This code works only on editor page.
– KAGG Design
yesterday




This code works only on editor page.
– KAGG Design
yesterday












What about enqueue_block_assets?
– Marc
14 hours ago




What about enqueue_block_assets?
– Marc
14 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to WordPress Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f320653%2fhow-to-detect-the-usage-of-gutenberg%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater