I have a custom post type with a custom Gutenberg template to only display a custom Gutenberg block. This is where the user configures things.
I have another block for all other post types where the user can reference one of the custom post types to display in a post, page, etc.
I'm using render_callback(), and everything works fine, except the rendered output on a post, page, etc, does not update when the user updates the configuration in the custom post type. The preview/front-end of the custom post type updates fine when saved. But anything referencing it seems to be stuck at the time it was referenced and won't display updates unless the user removes the block and re-selects it.
How can I propagate the updates from the custom post type render_callback() output to the regular posts that reference it?
I'm not even sure what to Google search for in this case.
Edit: Was asked for some code.
plugin.php
public function block_init() {
register_block_type( __DIR__ . '/build', array(
'render_callback' => array($this, 'render_callback_dynamic')
) );
}
public function render_callback_dynamic ($block_attributes, $content) {
ob_start();
include 'blocks/template.php';
$output = ob_get_clean();
if ( $output === '' ) {
$output = (string) $content;
}
return $output;
}
Edit.js
import { useSelect } from "@wordpress/data";
import { CUSTOM_POST_TYPE } from '../../constants'
import Editor from '../Editor'
import Selector from '../Selector'
const Edit = (props) => {
const { isCustomPostType } = useSelect((select) => {
const { getCurrentPostType } = select("core/editor");
return {
isCustomPostType: getCurrentPostType() === CUSTOM_POST_TYPE,
};
});
return isCustomPostType ? <Editor {...props} /> : <Selector {...props} />
}
export default Edit
The thinking here is that the Edit component returns either the configuration component or the selector component if on the custom post type or a regular post.
When the configuration changes on the custom post type page, the front-end view of the custom post type does reflect the changes. It is just that the references in a regular post are not updating unless the block is removed and re-added. No caching is being used.