Use dynamic blocks when you want to write your own custom PHP code to control the front-end output of a block. This can be useful for a number of scenarios such as:
- You want to add dynamic content to your block such as looping through a custom post type
- You want to avoid invalid content warnings
Dynamic Block Considerations
Before creating a dynamic block, you should consider a few things:
- Dynamic blocks do not save content to the database, instead the content is generated dynamically on-the-fly when the page is loaded
- This means that you will not be able to use sources other than block meta for your attributes as there is no content stored in the database that can be used to extract attribute values
- Avoid changing blocks between dynamic and non-dynamic; it’s best to decide early-on (before your block is added to pages) whether you would like it to be dynamic or not
- Changing an existing block to dynamic may result in data loss. If you decide to make an existing block dynamic you should: 1) ensure all attributes are using the block meta source, and 2) edit and re-save any page that is already using the block
Creating Dynamic Blocks
Any block can be made dynamic by checking the “Make block dynamic” box on the Settings screen. A message will be displayed telling you where to add your block’s PHP file. Create a PHP file with the name shown in the message inside of a wbb-blocks folder within your site’s theme.
Some things to note about dynamic blocks:
- The PHP code you enter for your dynamic block is only used to generate the front-end view of your block. You still need to create an editor view for the block to appear in the WordPress editor
- The Front-end View screen in Wicked Block Builder is ignored for dynamic blocks
- You must use the block meta source for all attributes (other attribute sources will not work)
Accessing Your Block’s Attributes
You can access your block’s attributes by using the
$attributes variable inside your block’s PHP.
Note: if the
$attributes variable is empty, double-check that your attributes are using the block meta source.
Note: in version 1.4.0 and earlier, attributes were passed to blocks via
If your dynamic block doesn’t appear when viewing a page:
- Check the name of your block’s PHP file. Make sure it matches the filename shown on the block’s Settings screen
- Check your block for PHP errors. Depending on your environment and the type of error, errors may not be visible. Enable debugging and/or logging for your WordPress site to track down the error