Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Backoffice - Gutenberg blocks displayed by default, non-removable, non-movable WordPress

  • SOLVED

I would like gutenberg blocks to be displayed, by default, in the backoffice of certain pages or certain CPTs, in a predefined order. Knowing that among my gutenberg blocks, there are blocks created from ACF (via acf_register_block_type)

The blocks already in place cannot be deleted or moved.

Scenario:

I create a new page or a new publication in the "edito" CPT, the edition of the publication opens. There are gutenberg blocks ready to be edited which are already placed in a specific order.

In attachment, the backoffice as it would be when I want to create a new page. In this example, there would be three blocks ready for editing: image, banner and search field.

Answers (2)

2020-05-18

Reigel Gallarde answers:

function myplugin_register_template() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'myplugin_register_template' );


change post in get_post_type_object( 'post' ); to your custom post type.

$post_type_object->template will hold an array of blocks that will be used as default when creating a post in this post type.

$post_type_object->template_lock = 'all'; will do the trick in non-removable, non-movable.

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


Sébastien | French WordpressDesigner comments:

so cool ! perfect ????

and do you know how could I forced the mode « edition » for a block instead of the preview mode ?


Reigel Gallarde comments:

in acf_register_block_type, you can set mode to "edit".

mode
(String) (Optional) The display mode for your block. Available settings are “auto”, “preview” and “edit”. Defaults to “preview”. auto: Preview is shown by default but changes to edit form when block is selected. preview: Preview is always shown. Edit form appears in sidebar when block is selected. edit: Edit form is always shown.

Note. When in “preview” or “edit” modes, an icon will appear in the block toolbar to toggle between modes.




reference: https://www.advancedcustomfields.com/resources/acf_register_block_type/

2020-05-18

Monit Jadhav answers:


/*Register WordPress Gutenberg CPT */
function cw_post_type() {

register_post_type( 'edition',
// WordPress CPT Options Start
array(
'labels' => array(
'name' => __( 'Edition' ),
'singular_name' => __( 'Edition' )
),
'has_archive' => true,
'public' => true,
'rewrite' => array('slug' => 'edition'),
'show_in_rest' => true,
'supports' => array('editor')
)
);
}

add_action( 'init', 'cw_post_type' );

/* Template with Fixed Gutenberg and other blocks like ACF block */

function fixed_template_blocks() {
$post_type_object = get_post_type_object( 'edition' );
$post_type_object->template = array(
array( 'core/cover' ),
array( 'core/heading' ),
array( 'core/subhead' ),
array( 'core/paragraph'),
array( 'core/image' ),
array( 'core/list'),
array( 'core/video'),
array( 'core/audio'),
array( 'core/embed'),

);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'fixed_template_blocks' );


I tried this with CPT and seems to be working like a charm. If you are using ACF plugin blocks you need to replace the core gutenberg block names with ACF block names.


array( 'core/heading' ) replace with array('acf_gutenberg_block_here)



get_post_type_object( 'edition' ); replace edition with your own custom post type name.


if you have names for your blocks and the custom post type name send it over I can update and resend the code

Monit


Sébastien | French WordpressDesigner comments:

so cool ! perfect ????

and do you know how could I forced the mode « edition » for a block instead of the preview mode ?