Replacing {merge_tag} in block
-
I am trying to create a custom block that will let users use merge tags in the content.
E.gToday is {my_block_date:y-m-d}
=>Today is 2022-3-16
.
(Ignore the specific example, some use cases are dynamic but most aren’t).I need to:
– show a replacement value on the preview/frontend.
– show a replacement value in the block editor
– indicate in the block editor where the merge tag is used. Either by decorating the replacement value or showing the{merge_tag}
when the block content is being edited.
– (bonus) submit the block to the block directory.Any idea how this can be do this?
## What I’ve tried:
– replacing the{merge_tage
} in php withadd_filter('render_block',...)
. You cant see the values in the editor, and my understanding of the block directory guidelines is to keep php empty except for theregister_block_type()
call.
– replacing the{merge_tag}
inedit.js
withsetAttributes( {content: replaceMergeTags( newContent )} )
. This is destructive, replacing the tag on the front and backend with no indicator it was ever used.
– replacing the{merge_tag}
inedit.js
with<RichText ... value={replaceMergeTags( newContent )} />
. This keeps the original tag in the attribute, and shows the replacement in the editor, but you still need to useadd_filter('render_block',...)
for the frontend, and no indicator of the tag was used in the editor.
- The topic ‘Replacing {merge_tag} in block’ is closed to new replies.