How do I get the current post ID within a Gutenberg/Block Editor block?

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(); // class component class … Read more

Disable Gutenberg text-Settings in all blocks

The closest I can find in the documentation is disabling custom font size (the text input) and forcing the dropdown font size to only contain “Normal”. add_action(‘after_setup_theme’, ‘wpse_remove_custom_colors’); function wpse_remove_custom_colors() { // removes the text box where users can enter custom pixel sizes add_theme_support(‘disable-custom-font-sizes’); // forces the dropdown for font sizes to only contain “normal” … Read more

Remove block styles in the Block Editor

We start off by finding out which block styles exists via .getBlockTypes(). This will dump it into the console: wp.domReady(() => { // find blocks styles wp.blocks.getBlockTypes().forEach((block) => { if (_.isArray(block[‘styles’])) { console.log(block.name, _.pluck(block[‘styles’], ‘name’)); } }); }); Example output: core/image (2) [“default”, “rounded”] core/quote (2) [“default”, “large”] core/button (2) [“fill”, “outline”] core/pullquote (2) [“default”, … Read more

Custom action button in Gutenberg editor (post_submitbox_misc_actions – equivalent)

wp_enqueue_script( ‘some-slug’, ‘/path/to/script.js’, array( ‘wp-edit-post’, ‘wp-plugins’, ‘wp-i18n’, ‘wp-element’ ), ‘0.1’ ); script.js: var el = wp.element.createElement; var __ = wp.i18n.__; var registerPlugin = wp.plugins.registerPlugin; var PluginPostStatusInfo = wp.editPost.PluginPostStatusInfo; var TextControl = wp.components.TextControl; function MyPostStatusInfoPlugin({}) { return el( PluginPostStatusInfo, { className: ‘my-post-status-info’ }, el( TextControl, { name: ‘my_edit_summary’, label: __( ‘Edit summary’ ), help: __( ‘Briefly … Read more

In Gutenberg, now that withAPIData is being deprecated, how can I do an async call to a custom endpoint?

It’s worth noting that the answer above is out of date. The data store should now look like this: const actions = { setUserRoles( userRoles ) { return { type: ‘SET_USER_ROLES’, userRoles, }; }, receiveUserRoles( path ) { return { type: ‘RECEIVE_USER_ROLES’, path, }; }, }; const store = registerStore( ‘matt-watson/secure-block’, { reducer( state = … Read more

get_post_gallery with Gutenberg

Using birgire’s suggestions, I came up with the following solution that is working well with both the old pre-WP 4.x data and the newer posts that have been added under 5.0. // if there is a gallery block do this if (has_block(‘gallery’, $post->post_content)) { $post_blocks = parse_blocks($post->post_content); $ids = $post_blocks[0][attrs][ids]; } // if there is … Read more