Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Gutenberg blocks in a Wordpress theme

Is it possible to include a custom Gutenberg block in a Wordpress theme. So that I doesn't have to upload two separate parts: Theme and a plugin with the Gutenberg block.

I have already tried to create a subfolder with the Gutenberg plugin inside the theme and include this one in the functions.php file of my theme. But that didn't work.

like image 874
mrtf Avatar asked Oct 25 '25 18:10

mrtf


1 Answers

I see that you're a new contributor; welcome!

The following should work: Put the PHP that registers the block in the theme's functions.php file. You'll have to write a function that hooks into the init action and does the following:

  • uses wp_register_script() to designate a JavaScript file that will act as the template for your block;
  • uses register_block_type() to register your block.

That template file, which you would put in the theme alongside functions.php or in a sub-folder, will use JSX to format your block.

Of course, the details of what you code depend on what you want your block to do. I strongly suggest you go through the official Gutenberg Block Tutorial from WordPress.

like image 63
nydame Avatar answered Oct 28 '25 02:10

nydame



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!