4
votes

I have been starting to testing out Gutenberg editor with both ACF and custom blocks. And I have been looking around to solve my problem but I couldn't find anything about this topic (Maybe my google skills is not good enought)

But my case is this: I have a custom post type where I want to set a template so they can't move around the blocks and add other blocks and so on. And on this post type around 70% is created by code. Because it is fetching all the information from an API.

Test 1: I have created an ACF block with all the fields I need, and it is working as it should when I create a new post from WP admin. But when I run the update_field function it is saving it to post_meta table as it did before. So my question here is how do I update a field so it saves it to post_content and not to post_meta table.

Test 2: I created custom blocks for all of the fields (convert each ACF field to and block) and set up the template to use these blocks only. But here I have no idea how update update post_content with PHP or Javascript.

I hope you can help me out with this :) If anything is unclear tell, and I will try to explain it

2

2 Answers

1
votes

ACF has an ability to pre-init fields before post will be visible to user on post creation page. You can try to use this function to set desired content to fields.

You can read about this here: https://www.advancedcustomfields.com/resources/acf-prepare_field/

0
votes

As of Wordpress 5.0.0

You can use template and template_lock arguments upon registering your custom post type.

You can then set an array of specific blocks to use and you can chose to restrict users from adding new blocks or removing them.

Attribute Description
template (array) Array of blocks to use as the default initial state for an editor session. Each item should be an array containing block name and optional attributes.
template_lock (string/false) Whether the block template should be locked if $template is set. If set to 'all', the user is unable to insert new blocks, move existing blocks and delete blocks. If set to 'insert', the user is able to move existing blocks but is unable to insert new blocks and delete blocks. Default false.

A short example would be something along the lines of...

<?php
$args = [
    //...
    'template_lock' => 'all',
    'template' => [
        [ 'core/paragraph' ],
        [ 'core/file' ],
        //...
    ],
    //...
];
register_post_type( $post_type, $args );
?>

Currently, Gutenberg documentation is scarced, you can find a complete list of blocks & parameters @ https://github.com/WordPress/gutenberg/tree/master/packages/block-library/src


Gutenberg is still in development, some features don't act as they should.