How to change the Default block in the block-editor away from the paragraph block?

When I open the block editor on a new post or page or edit an existing post or page, the Default Gutenberg Block is the paragraph block. How do I change this to a gallery block or another block?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use the Gutenberg Block Template it is used to have a content placeholder for your Gutenberg content.

https://developer.wordpress.org/block-editor/developers/block-api/block-templates/

<?php
function myplugin_register_template() {
    $post_type_object = get_post_type_object( 'post' );
    $post_type_object->template = array(
        array( 'core/image' ),
    );
}
add_action( 'init', 'myplugin_register_template' );

Method 2

This is possible with the setDefaultBlockName function although it is poorly documented at the moment.

You can try this out by placing this in the developers console of your web browser while you have the block editor open.

wp.domReady(() => {
  wp.blocks.setDefaultBlockName('core/quote');
});

(source)

I would recommend that you create a custom plugin; and start with this (haven’t tested, but should work).

function change_default_block {
wp_register_script( 'js-change-default-block', plugin_dir_path( __FILE__ ) . '/js/change-default-block.js', '', '', true );
}

add_action( 'wp_enqueue_scripts', 'change_default_block', 4 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x