We’re rewarding the question askers & reputations are being recalculated! Read more.
29

For Gutenberg to work in a Custom Post Type you need to enable both the editor in supports (which you already have) and show_in_rest. So add 'show_in_rest' => true, to your post registration arguments array.


22

WordPress 5.0+ has a function for this: parse_blocks(). To see if the first block in the post is the Heading block, you'd do this: $post = get_post(); if ( has_blocks( $post->post_content ) ) { $blocks = parse_blocks( $post->post_content ); if ( $blocks[0]['blockName'] === 'core/heading' ) { } }


16

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is possible but appears to be a bit more involved. Certain classes and markup are required in the output for the block to be able to parse the content and keep it ...


11

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 ...


11

Update #1: After asking from @leymannx I checked how these settings are stored. It turned out that settings are not permanent, they are saved in the browser as localStorage. key: WP_DATA_USER_{id}: value: { "core/nux":{ "preferences":{ "areTipsEnabled":false, "dismissedTips":{} } }, //"core/edit-post" ...


10

There is is_gutenberg_page() function that'll be present when you activate Gutenberg, so you may check: if( function_exists( 'is_gutenberg_page' ) ) This will only check if Gutenberg is activated, and the function itself will check if the current editor is set to load Gutenberg. So the CODE becomes: if( function_exists( 'is_gutenberg_page' ) && ...


10

There's probably a simpler way to do this, and if there isn't you should open an issue on the Gutenberg GH issue tracker ( the API is not set in stone ). Because the API is not set in stone, this answer may be useless when it's finally done and merged. The first and best place to ask GB questions is on GitHub You can eliminate all blocks using this: wp....


9

Answering my own question: As suggested by WebElaine I created a completely new block to fix the problem. To make this task easier, I used create-guten-block by Ahmad Awais, which is pretty easy to set up. In init.php I made sure to enqueue my JavaScript and extra CSS for the frontend. This should be straight forward. I recommend reading the Learning ...


9

It's nothing new with the block editor, it's the same age-old way by putting the following code into your theme's functions.php: add_action( 'init', 'wpse325327_add_excerpts_to_pages' ); function wpse325327_add_excerpts_to_pages() { add_post_type_support( 'page', 'excerpt' ); } Here's my screenshot in a fresh WordPress 5.0.3 install:


9

const { registerPlugin } = wp.plugins; const { PluginPrePublishPanel } = wp.editPost; const { select, dispatch } = wp.data; const { count } = wp.wordcount; const { serialize } = wp.blocks; const { PanelBody } = wp.components; const PrePublishCheckList = () => { let lockPost = false; // Get the WordCount const blocks = select( 'core/editor' )....


8

You have second and third arguments with this hook you can use the second to get the block type. https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/ A filter that applies to all blocks returning a WP Element in the save function. This filter is used to add extra props to the root element of the save function. For example: to add ...


7

Digging myself deeper in documentation I got the following result. There is a way to group your custom blocks around a given category in Gutenberg, and therefore we have the method block_categories. So with a filter you can extend the default categories with custom ones. Here is my example: add_filter( 'block_categories', function( $categories, $post ) { ...


7

First you need to import the component - const { PanelColorSettings, } = wp.editor; then inside the InspectorControls you call the component <PanelColorSettings title={ __( 'Color Settings' ) } colorSettings={ [ { value: color, ...


7

There's a whitelist blocks removal example from the Gutenberg Handbook: var allowedBlocks = [ 'core/paragraph', 'core/image', 'core/html', 'core/freeform' ]; wp.blocks.getBlockTypes().forEach( function( blockType ) { if ( allowedBlocks.indexOf( blockType.name ) === -1 ) { wp.blocks.unregisterBlockType( blockType.name ); } } )...


7

Changing the "Post reverted to draft." text In WordPress version 5.0, WordPress introduced a new post type label which has the key item_reverted_to_draft and it's defined as: item_reverted_to_draft – Label used when an item is switched to a draft. Default is ‘Post reverted to draft.’ / ‘Page reverted to draft.’ Hence, as with any other post type ...


6

It took me way too long to find this but all you have to do is set SCRIPT_DEBUG to true. Simply add this line to wp-config.php: define('SCRIPT_DEBUG', true);


6

TextControl is from wp.components not wp.blocks. const { registerBlockType, RichText, AlignmentToolbar, BlockControls, InspectorControls, } = wp.blocks; const { TextControl } = wp.components; Changing that I was able to get your block to work fine instead of error in Gutenberg 2.2.0.


6

In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood. Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on ...


6

I face the same as your problem and solved as the following you must add 'show_in_rest' => true, for both post_type and taxonomies int the last line of array such as register_post_type( 'portfolio', array( 'labels' => $labels, 'exclude_from_search' => false, 'has_archive' => true, 'public' => true, '...


6

You should add a custom plugin. That's need a PHP main file that include, register a JavaScript file. The source below should result in an plugin. You find a usable solution also in the probs below. PHP part add_action( 'enqueue_block_editor_assets', 'my_gutenberg_scripts' ); function my_gutenberg_scripts() { wp_register_script( 'my-editor-...


6

Switching Gutenberg to fullscreen mode requires setting to TRUE fullscreenMode from the core/edit-post package. To enqueue appropriate script, you use enqueue_block_editor_assets action hook. function se337302_fullscreen_editor() { $js_code = "jQuery(document).ready(function(){" . " var isFullScreenMode = wp.data.select('core/edit-post')....


5

The function is_gutenberg_page is from the Gutenberg plugin, while the is_block_editor method is available from 5.0. This function below combines both into a single checking function. The code below is from Freemius SDK, props to their team: function is_gutenberg_page() { if ( function_exists( 'is_gutenberg_page' ) && ...


5

InspectorControls is deprecated as of v2.4 https://github.com/WordPress/gutenberg/blob/9ebb918176c1efd0c41561d67ea3273bae5d3d6a/docs/deprecated.md#L15 wp.blocks.InspectorControls.* components removed. Please use wp.components.* components instead. This is right at the top of your code: const { registerBlockType, RichText, AlignmentToolbar, ...


5

Everything works for me with allowed_block_types hook. Example: add_filter( 'allowed_block_types', 'my_function' ); function my_function( $allowed_block_types ) { return array( 'core/paragraph' ); } You can insert the above code to your functions.php file to a custom plugin. It removes all blocks except the Paragraph block. More ...


5

You don't need apiFetch or localization to get a list of all categories. You can do this with the wp.data module: wp.data.select('core').getEntityRecords('taxonomy', 'category'); See the Gutenberg Handbook entry on Core Data for more details.


5

Here's an untested suggestion, to remove an HTML block of type: <!-- wp:block/timed-block {"dateFrom":"2018-11-14", "dateTo":"2018-12-18"} --> <section> ... </section> <!-- /wp:block/timed-block --> from the rendered content, using the render_block filter (available in 5.0+): add_filter( 'render_block', function( $...


5

To simply get the value you can call the selector: const post_id = wp.data.select("core/editor").getCurrentPostId(); In this case, the post id won't change, so you could assign the value to a constant outside of the component: const { Component } = wp.element; const { getCurrentPostId } = wp.data.select("core/editor"); const post_id = getCurrentPostId(); ...


5

Yes, you can disable it. You can do this with code If you want to disable it globally, you can use this code: if ( version_compare($GLOBALS['wp_version'], '5.0-beta', '>') ) { // WP > 5 beta add_filter( 'use_block_editor_for_post_type', '__return_false', 100 ); } else { // WP < 5 beta add_filter( 'gutenberg_can_edit_post_type', '...


5

Actually it can be done with the filter: const { createHigherOrderComponent } = wp.compose; const withCustomClassName = createHigherOrderComponent( ( BlockListBlock ) => { return ( props ) => { if(props.attributes.size) { return <BlockListBlock { ...props } className={ "block-" + props.attributes.size } />; } else ...


5

AFAIK there is only one way to remove blocks from Gutenberg - you have to use allowed_block_types filter. Unfortunately Gutenberg developers are not very familiar with WordPress hooks and filters, so they created a little monster with this one. So instead of passing a list of core blocks in there, they pass true to enable all blocks. That way you're unable ...


Only top voted, non community-wiki answers of a minimum length are eligible