Slide 1

Slide 1 text

Getting Started with Gutenberg Blocks

Slide 2

Slide 2 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • I’m a Senior Developer/co-founder at 11 online • Started Block Party (https://wpblock.party) this year • I first tried WordPress in 2004, started using it more in 2006, started really developing for WordPress in 2013 • These days I work mostly in React.js on the front end and either python (Flask) or PHP (Lumen) on the backend. I also do some Android dev and, of course, WordPress. I started using React.js over 2 years ago. About me - Eric Debelak

Slide 3

Slide 3 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • New WordPress editor as of...? • A What You See Is What You Get (WYSIWYG) Experience • Content paradigm is BLOCKS! • Comes with simple blocks, but you can build custom blocks What is Gutenberg?

Slide 4

Slide 4 text

DEMO

Slide 5

Slide 5 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Do you have branding guidelines to follow? If so, Gutenberg by default gives you too much freedom Why Make Custom Blocks

Slide 6

Slide 6 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • What you can do with the default paragraph block: • Content creators should be focusing on creating content, not making styling choices Why Make Custom Blocks

Slide 7

Slide 7 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Gutenberg is written in React.js, a modern front-end library from Facebook https://reactjs.org/ • Most examples will use the ES6 and JSX syntax, which makes writing React.js much easier • Gutenberg uses attributes to interact with blocks • Blocks are then saved in the post content as HTML, JSON data in HTML comments or a combination of the two Basics

Slide 8

Slide 8 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • When you register a block, you have an edit method and a save method • The save method is responsible for turning the attributes into HTML that will then be shown on posts and pages Basics (continued)

Slide 9

Slide 9 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • The attributes are saved as JSON in html comments unless they are attached to an html element Basics (continued)

Slide 10

Slide 10 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • On the editor side, Gutenberg reads the attributes for each block, and then sets the editor method with those attributes • The editor method then allows users to interact with the block and change the attributes • The editor method can include a preview of what the block will look like in the save method Basics (continued)

Slide 11

Slide 11 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Gutenberg allows for templating as well - https://wordpress.org/ gutenberg/handbook/templates/ • Add in attributes and defaults • And you can lock the templates Basics (continued) - Templates

Slide 12

Slide 12 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ Resources: https://wordpress.org/gutenberg/handbook/ https://github.com/WordPress/gutenberg Basics (continued)

Slide 13

Slide 13 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • We can’t cover everything here, but hopefully we’ll cover enough for you to get started and follow the example • Modern JavaScript requires at the very least build processes. Browsers can’t read JSX or ES6 and the code needs to be compiled into what browsers can read. • To set up these build processes, we need Node.js and npm (Node Package Manager). https://nodejs.org/en/ Getting Started

Slide 14

Slide 14 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Once we have these tools, we can set up Babel or Webpack for our build process or use a zero-configuration tool to help us • Create Guten Block (https://github.com/ahmadawais/create-guten-block) Getting Started (continued) create-guten-block my-block cd my-block npm start

Slide 15

Slide 15 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Some Basic ES6 - arrow functions Getting Started (continued) function(param) { } (param) => { } • mutable and immutable variable assignments let a = 1; //mutable const b = 2; //immutable

Slide 16

Slide 16 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ Getting Started (continued) • destructuring const { keyOne, keyTwo } = objectVariable; var keyOne = objectVariable.keyOne; var keyTwo = objectVariable.keyTwo;

Slide 17

Slide 17 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Some Basic JSX - XML like JS syntax - our edit and save methods must return either null or a JSX element (or vanilla JS equivalent) Getting Started (continued) const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, world!' ); const element = (

Hello, world!

);

Slide 18

Slide 18 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • setAttributes - function to change a blocks attributes • isSelected - see if the user has selected your block • Editor components: • Plain Text, Rich Text, Media Upload, Color Palette, Dropdown, Inner Blocks, Select Control, Toggle Control, and more. • Complete list - https://github.com/WordPress/gutenberg/tree/master/ components and https://github.com/WordPress/gutenberg/tree/master/ blocks Gutenberg - What’s built in

Slide 19

Slide 19 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Need some help with your edit methods? • Node Package Manager - npm - https://www.npmjs.com/ • npm has 650,000 packages. • Very useful for extending default functionality. Adding More

Slide 20

Slide 20 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • https://github.com/11online/gutenberg-simple-paragraph Going over a simple example

Slide 21

Slide 21 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • directory structure of where we’ll be working • also includes a dist directory for the compiled code Going over a simple example

Slide 22

Slide 22 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - imports and variable assignments Going over a simple example

Slide 23

Slide 23 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - registerBlockType part 1 Going over a simple example

Slide 24

Slide 24 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - registerBlockType part 2 Going over a simple example

Slide 25

Slide 25 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - edit part 1 Going over a simple example

Slide 26

Slide 26 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - edit part 2

Slide 27

Slide 27 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • src/block/block.js - save Going over a simple example

Slide 28

Slide 28 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ Our approach (in development) for client sites • https://github.com/11online/11online-blocks Advanced Block Demos • https://github.com/11online/gutenberg-simple-statistics • https://github.com/11online/gutenberg-simple-weather-block More advanced examples for the curious

Slide 29

Slide 29 text

@EricDebelak [email protected] Slides: https://11online.us/getting-started-building-gutenberg-blocks/ • Validation - Gutenberg revalidates both the html and the attributes, so you need default attributes or selectors for everything • Arrays don’t allow for declaration of sub attributes • setAttributes has no callback! • Using out of the box components do not always look like the front end of the site will (PlainText, RichText) • Inner Blocks are really useful, but you can only use one per block Gotchas

Slide 30

Slide 30 text

QUESTIONS? @EricDebelak [email protected]