Skip to content

Commit

Permalink
Add ESNext syntax to meta block tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaz committed Feb 19, 2019
1 parent 929afdd commit b6eae18
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ By specifying the source of the attributes as `meta`, the Block Editor automatic

Add this code to your JavaScript file (this tutorial will call the file `myguten.js`):

{% codetabs %}
{% ES5 %}
```js
( function( wp ) {
var el = wp.element.createElement;
Expand Down Expand Up @@ -56,6 +58,49 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten
});
})( window.wp );
```
{% ESNext %}
```jsx
const { registerBlockType } = wp.blocks;
const TextField = wp.components.TextControl;

registerBlockType("myguten/meta-block", {
title: "Meta Block",
icon: "smiley",
category: "common",

attributes: {
blockValue: {
type: "string",
source: "meta",
meta: "myguten_meta_block_field"
}
},

edit( { className, setAttributes, attributes } ) {

function updateBlockValue( blockValue ) {
setAttributes({ blockValue });
}

return (
<div className={ className }>
<TextField
label="Meta Block Field"
value={ attributes }
onChange={ updateBlockValue }
/>
</div>
);
},

// No information saved to the block
// Data is saved to post meta via attributes
save() {
return null;
}
});
```
{% end %}

**Important:** Before you test, you need to enqueue your JavaScript file and its dependencies. Note the WordPress packages used above are `wp.element`, `wp.blocks`, and `wp.components`. Each of these need to be included in the array of dependencies. Update the `myguten-meta-block.php` file adding the enqueue function:

Expand Down

0 comments on commit b6eae18

Please sign in to comment.