render.php (Frontend Template)

The render.php file (or any other file defined in the render property of block.json) defines the server-side process that returns the markup for the block when there is a request from the front end. If defined, this file will take precedence over other ways to render the block’s markup on the front end.

Note: render.php is only used by Dynamic Blocks. None of the block’s markup is saved to the database. Instead, its attributes are written inside the block indicator comment. The render method is called to create the block’s markup whenever a front-end user visits the page. This differs from Static Blocks, who store their entire markup in the database.

These render methods automatically receive attribute and inner content information. They can also get additional required dynamic site information – post lists, comments, taxonomy information, etc.

Alternatively, the block registration function includes a render_callback argument. As the recommended practice is to register a block type using the metadata stored in the block.json file, we will not cover this method.

Example

@todo embed https://github.com/bu-ist/labs-theme-blocks/blob/sandbox/src/blocks/theme/render.php

Overriding

The output of a block can be modified with php using either the render_block_{$this->name} or render_block filters. An example of a theme modifying a plugin-based block can be seen here. Code follows:

@todo embed https://github.com/bu-ist/labs-theme-blocks/blob/sandbox/includes/blocks/render/index.php

Further Reading