Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active February 3, 2018 03:00
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 davidsword/7df8e120b06740ee333c0167e01dfae6 to your computer and use it in GitHub Desktop.
Save davidsword/7df8e120b06740ee333c0167e01dfae6 to your computer and use it in GitHub Desktop.
Gutenberg hello world
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
withAPIData = wp.components.withAPIData;
registerBlockType( 'sandbox/myblock', {
title: 'My Block',
icon: 'archive',
category: 'widgets',
// representation of the block in the editor's context
edit: function( props ) {
return el(
'div',
{},
"This is the editors content."
);
},
// content to save to databse, use on front end
save: function() {
return "This is the saved front-end HTML content."; // which can use PHP output with proper filters
},
} );
<?php
add_action( 'enqueue_block_editor_assets', function () {
wp_enqueue_script(
'sandbox_myblock',
plugins_url( 'myblock.js', __FILE__ ),
array( 'wp-blocks', 'wp-i18n', 'wp-element' ),
filemtime( plugin_dir_path( __FILE__ ) . 'myblock.js' )
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment