Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESNext syntax to meta block tutorial #13954

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,40 @@ 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;
var registerBlockType = wp.blocks.registerBlockType;
var TextField = wp.components.TextControl;
var TextControl = wp.components.TextControl;

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

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

edit: function(props) {
edit: function( props ) {
var className = props.className;
var setAttributes = props.setAttributes;

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

return el(
"div",
return el(
'div',
{ className: className },
el( TextField, {
label: "Meta Block Field",
el( TextControl, {
label: 'Meta Block Field',
value: props.attributes.blockValue,
onChange: updateBlockValue
} )
Expand All @@ -53,9 +55,52 @@ Add this code to your JavaScript file (this tutorial will call the file `myguten
save: function() {
return null;
}
});
})( window.wp );
} );
} )( window.wp );
```
{% ESNext %}
```jsx
const { registerBlockType } = wp.blocks;
mkaz marked this conversation as resolved.
Show resolved Hide resolved
const { TextControl } = wp.components;
mkaz marked this conversation as resolved.
Show resolved Hide resolved

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 }>
<TextControl
label="Meta Block Field"
value={ attributes.blockValue }
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