How to call Gutenberg Blocks anywehre via PHP?
To call Gutenberg blocks via PHP, you can use the render_block()
function provided by WordPress. This function allows you to programmatically render a specific Gutenberg block or a group of blocks. Here’s an example of how you can do it:
- Determine the block(s) you want to render. You can specify the block name(s) or use a block’s unique identifier(s). For example, let’s say you want to render a block named “my-custom-block” and another block with the identifier “12345”:
$blocks = array(
array(
'name' => 'my-custom-block',
),
array(
'blockName' => 'core/block',
'attrs' => array(
'ref' => '12345',
),
),
);
- Use the
render_block()
function to render the blocks. Pass the array of blocks as an argument to the function:
$output = render_block($blocks);
The $output
variable will now contain the rendered HTML output of the specified blocks.
- Display the rendered blocks by echoing the
$output
variable wherever you want the blocks to appear on your website:
echo $output;
By calling render_block()
, you can dynamically generate and display Gutenberg blocks in your PHP code. This can be useful when you need to customize the rendering of blocks or integrate them into specific parts of your WordPress theme or plugin.