Skip to content

Instantly share code, notes, and snippets.

@lotharthesavior
Last active September 14, 2020 02:23
Show Gist options
  • Save lotharthesavior/2d536e2f25ee86f57c259e87bf7d4354 to your computer and use it in GitHub Desktop.
Save lotharthesavior/2d536e2f25ee86f57c259e87bf7d4354 to your computer and use it in GitHub Desktop.
Gutenberg Base Block
var wpddb = {}
wpddb.contentTag = 'p';
wpddb.contentClass = 'wpddb-content';
(function (blocks, editor, element) {
var el = wp.element.createElement;
var RichText = editor.RichText;
blocks.registerBlockType('wpddb/promotion', {
title: 'WPDDB Promotion',
icon: 'universal-access-alt',
category: 'layout',
attributes: {
content: {
type: 'array',
source: 'children',
selector: '.' + wpddb.contentClass, // using specific selector
},
},
edit: function (props) {
var content = props.attributes.content;
function onChangeContent(newContent) {
props.setAttributes({content: newContent});
}
return el(
'div',
{},
[
el('h1', {
key: 'heading1',
}, 'Back end'),
el(
RichText,
{
tagName: wpddb.contentTag,
value: content,
onChange: onChangeContent,
className: wpddb.contentClass,
key: 'body1',
}
)
]
);
},
save: function (props) {
return el(
'div',
{tagName: wpddb.contentTag},
[
el('h1', {key: 'heading1'}, 'Front end.'),
el(RichText.Content, {
value: props.attributes.content,
className: wpddb.contentClass,
key: 'body1',
})
]
);
},
});
}(
window.wp.blocks,
window.wp.blockEditor,
window.wp.element,
));
<?php
/**
* Plugin Name: WT Dev Depth Block
*/
if (!defined('WPDDB_PROMOTION_BLOCK_SCRIPT')) {
define('WPDDB_PROMOTION_BLOCK_SCRIPT', 'wpddb-promotion');
}
function wpddb_block_init()
{
wp_register_script(
WPDDB_PROMOTION_BLOCK_SCRIPT, // the name of our block script
plugins_url('block.js', __FILE__), // the javascript to be enqueued
['wp-blocks', 'wp-element', 'wp-editor'] // dependencies
);
register_block_type('wpddb/promotion', [
'editor_script' => WPDDB_PROMOTION_BLOCK_SCRIPT,
]);
}
add_action('init', 'wpddb_block_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment