Include the Genesis Custom Block definition as part of the plugin to simplify distribution.
Earlier this month I showed how to include the template for a Genesis Custom Block in a plugin. This doesn’t solve the issue of the block definition. I looked into that.
It’s a CPT
I noticed that when you edit a block that it is a CPT with a post type of ‘genesis_custom_block‘. When I used Tools/Export I was able to export the block by choosing (the slightly confusingly named) “Custom Blocks“
From the exported file I was able to extract the ‘content:encoded‘ data which is the block definition. You need to remove the ‘<![CDATA[‘ and ‘]]>‘ wrappers.
Insert post on activation
I then experimented with calling wp_insert_post() when the plugin is activated (via register_activation_hook()). I did a lot of experimenting by writing a shortcode that used get_posts() to check whether the block already existed before converting the code into an activation function. It’s quite simple.
Now you can create your block in the Dashboad, export it and extract the definition and put that definition into the plugin code. I would like to add a meta box to the ‘Edit Block‘ page to expose the content. That’s for another day.
With the definition in the plugin code you can now bundle it and the block template and easily distribute them.