Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created September 28, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/0ae6953f4b9a7ee50f2d5e8abc2856bd to your computer and use it in GitHub Desktop.
Save damiencarbery/0ae6953f4b9a7ee50f2d5e8abc2856bd to your computer and use it in GitHub Desktop.
Distribute Genesis Custom Block in a plugin - Include the Genesis Custom Block definition as part of the plugin to simplify distribution. https://www.damiencarbery.com/2020/09/distribute-genesis-custom-block-in-a-plugin/
<item>
<title>Accordion block</title>
<link>https://example.com/genesis_custom_block/accordion-block/</link>
<pubDate>Tue, 15 Sep 2020 12:16:12 +0000</pubDate>
<dc:creator><![CDATA[me]></dc:creator>
<guid isPermaLink="false">https://example.com/?post_type=genesis_custom_block&#038;p=8</guid>
<description></description>
<content:encoded><![CDATA[{"genesis-custom-blocks\/accordion-block":{"name":"accordion-block","title":"Accordion block","excluded":[],"icon":"list","category":{"slug":"widgets","title":"Widgets","icon":null},"keywords":[""],"fields":{"title":{"name":"title","label":"Title","control":"text","type":"string","order":0,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null},"content":{"name":"content","label":"Content","control":"textarea","type":"textarea","order":1,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null,"number_rows":4,"new_lines":"autop"}}}}]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>8</wp:post_id>
<wp:post_date><![CDATA[2020-09-15 12:16:12]]></wp:post_date>
<wp:post_date_gmt><![CDATA[2020-09-15 12:16:12]]></wp:post_date_gmt>
<wp:comment_status><![CDATA[closed]]></wp:comment_status>
<wp:ping_status><![CDATA[closed]]></wp:ping_status>
<wp:post_name><![CDATA[accordion-block]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[genesis_custom_block]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:postmeta>
<wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
</item>
<details class="genesis-custom-block <?php block_field('className'); ?>">
<summary class="item-title"><?php block_field( 'title' ); ?></summary>
<div class="item-content"><p><?php block_field( 'content' ); ?></p></div>
</details>
<?php
/*
Plugin Name: Accordion block - Genesis Custom Blocks
Plugin URI: https://www.damiencarbery.com/2020/09/distribute-genesis-custom-block-in-a-plugin/
Description: Include the Genesis Custom Block definition as part of the plugin to simplify distribution.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'genesis_custom_blocks_template_path', 'dcwd_gcb_accordion_block_template_path', 10, 2 );
function dcwd_gcb_accordion_block_template_path( $path, $template_names ) {
foreach ( (array) $template_names as $template_name ) {
if ( 'blocks/block-accordion-block.php' == $template_name ) {
return __DIR__;
}
}
return $path;
}
register_activation_hook( __FILE__, 'insert_gcb_accordion_block' );
function insert_gcb_accordion_block() {
$block_title = 'Accordion block';
$block_name = sanitize_title( $block_title );
$block_content = '{"genesis-custom-blocks\/accordion-block":{"name":"accordion-block","title":"Accordion block","excluded":[],"icon":"list","category":{"slug":"widgets","title":"Widgets","icon":null},"keywords":[""],"fields":{"title":{"name":"title","label":"Title","control":"text","type":"string","order":0,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null},"content":{"name":"content","label":"Content","control":"textarea","type":"textarea","order":1,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null,"number_rows":4,"new_lines":"autop"}}}}';
$existing_blocks = get_posts( array( 'name' => $block_name, 'fields' => 'ids', 'post_type' => 'genesis_custom_block', ) );
// Quit if block already registered.
if ( count( $existing_blocks ) ) {
return;
}
$post = array(
'post_title' => $block_title,
'post_name' => $block_name,
'post_content' => $block_content,
'post_type' => 'genesis_custom_block',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$post_id = wp_insert_post( $post );
if ( is_wp_error( $post_id ) ) {
error_log( 'Error creating post: ' . $post_id->get_error_message() );
}
}
add_shortcode( 'gcb_add_accordion_block', 'gcb_add_accordion_block' );
function gcb_add_accordion_block() {
return '<p>No code yet</p>';
// See import_blocks() in genesis-custom-blocks/php/Admin/Import.php
$block_title = 'Accordion block';
$block_name = sanitize_title( $block_title );
$block_content = '{"genesis-custom-blocks\/accordion-block":{"name":"accordion-block","title":"Accordion block","excluded":[],"icon":"list","category":{"slug":"widgets","title":"Widgets","icon":null},"keywords":[""],"fields":{"title":{"name":"title","label":"Title","control":"text","type":"string","order":0,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null},"content":{"name":"content","label":"Content","control":"textarea","type":"textarea","order":1,"location":"editor","width":"100","help":"","default":"","placeholder":"","maxlength":null,"number_rows":4,"new_lines":"autop"}}}}';
$existing_blocks = get_posts( array( 'name' => $block_name, 'fields' => 'ids', 'post_type' => 'genesis_custom_block', ) );
if ( count( $existing_blocks ) ) {
return '<p>Block already registered</p>';
}
//else {
// return '<p>Block to be uploaded</p>';
//}*
//return '<pre>' . var_export( $existing_blocks, true ) . '</pre>';
$post = array(
'post_title' => $block_title,
'post_name' => $block_name,
'post_content' => $block_content,
'post_type' => 'genesis_custom_block',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$post_id = wp_insert_post( $post );
if ( !is_wp_error( $post_id ) ) {
return '<p>Post created</p>';
}
else {
return '<p>Error creating post: ' . $post_id->get_error_message() . '</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment