How to re-render static block from a dynamic block without saving it in the data
-
I need my gutenberg wordpress dynamic block to render all paragraph blocks in the page (just for display, and not in database).
<?php function create_block_init() { register_block_type(__DIR__ . '/build', array( 'render_callback' => 'render_my_widget' )); } function replaceTextInParagraphs($newValToSet) { $post = get_post(); $content = $post->post_content; if (has_blocks($content)) { $blocks = parse_blocks($content); for ($i = 0; $i <= count($blocks); $i++) { $block = $blocks[$i]; if ($block['blockName'] === "core/paragraph") { if (!empty($block['innerHTML'])) { $block['innerHTML'] = str_replace("foo", $newValToSet, $block['innerHTML']); } if (!empty($block['innerContent']) && is_array($block['innerContent'])) { $block['innerContent'] = array_map(function ($item) use ($newValToSet) { return str_replace("foo", $newValToSet, $item['innerHTML']); }, $block['innerContent']); } } } } return $content; } function render_my_widget($attributes, $content) { $newValToSet = $attributes['newVal']; $newContent = replaceTextInParagraphs($newValToSet); // How do i re render the content of the post??? }
I want basically replace some text in the paragraphs based on the current widget attribute. I am new to wordpress and not sure that what i am doing is the best option to achieve it. Also the example is simplified version. I need this block to be dynamic (it relies on URL passed params). The question is how to re-write the paragraphs after i replaced the text? Hope it make sense. thanks.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to re-render static block from a dynamic block without saving it in the data’ is closed to new replies.