Gutenberg Blocks
Simple WordPress plugin with a group of custom Gutenberg blocks written with Javascript and JSX support
by The WordPress Contributors · github.com/adrianahdez/gutenberg-blocks
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/adrianahdez/gutenberg-blocks/archive/refs/heads/main.zipSimple WordPress plugin with a group of custom reusable Gutenberg blocks to use on any project. Written with PHP, Javascript and JSX support – build step required.
Installation
- Upload the plugin files to the
/wp-content/plugins/gutenberg-blocksdirectory. - Activate the plugin through the 'Plugins' screen in WordPress
- Build assets: Go to the plugin folder from terminal, and run these commands (
v19.6.0of Node is recommended):npm install gulp npm run build - Enjoy!
Contributing
You are welcome to contribute with this project. You can create a fork from this repository and send PRs or open issues here.
How we reuse React Components in dynamic blocks
We build our dynamic blocks as a React component that isn't aware of Gutenberg. That block accepts a prop that is used to pass block attributes. But for now, Gutenberg only allow to render the dynamic blocks using PHP. So, to be able to reuse the same React Component that we have in the Edit function we based on this article from WooCommerce.
And what we do basically is:
- When the post or page is saved, only an empty
divelement with an ID or class is saved to the Database. - In the PHP
render_callback, we append the block attributes asdata-HTML attributes to thatdiv. So this is what ends up being rendered in the page. - We enqueue a new
frontend.jsfile which will import the React component. - From there, we just need to render the React component as you would render any other React app:
render(BlockComponent, divElement). We readdata-HTML attributes and pass them as props to the component.
License
WordPress is free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version.
Changelog
- 0.1.0
- Release