3
votes

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.

1

1 Answers

2
votes

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.