1 of 22

Creating your�First Block

Brian Richards / @rzen

2 of 22

Who still needs help with their local setup?

3 of 22

Add Josh’s companion plugin:

Update my companion plugin:

$> cd /app/public/wp-content/plugins/gutendev

$> git pull

$> cd /app/public/wp-content/plugins

$> git clone https://github.com/caldera-learn/alert-block.git

4 of 22

Create required files

Step 1

5 of 22

What we’ll need

  • Base PHP file to register assets
  • JS file for powering the block
  • (Optional) CSS for rendering front-end styles
  • (Optional) CSS for additional admin styles

6 of 22

Admin-only styles

Front-end (and admin) styles

01-static

assets

block.js

editor.css

style.css

index.php

JS file for powering the block

Base file to register assets

7 of 22

Register the assets

Step 2

8 of 22

What we’re registering

  • wp_register_script() – main block JS
  • wp_register_style() – front-end and admin styles
  • register_block_type() – attach assets to block

9 of 22

10 of 22

Create the rest of the block

Step 3

11 of 22

What we’re going to do...

  • Include external dependencies
  • Register a title
  • Add an icon*
  • Set the category
  • Add some keywords
  • Define edit functionality
  • Define save functionality

12 of 22

13 of 22

Code demo time.

14 of 22

And now for something you’ll really enjoy!

15 of 22

This seems like a lot of work for very little return...

— YOU, 30 SECONDS AGO

16 of 22

WP-CLI can accomplish this in 2 commands:

Plus one line of PHP:

$> wp scaffold plugin my-guten-block --skip-tests --activate

Success: Created plugin files.

$> wp scaffold block hello-block --title="Hello, Block!" --dashicon=smiley --category=common --plugin=my-guten-block

Success: Created block 'Hello, Block!'.

require_once plugin_dir_path( __FILE__ ) . '/blocks/hello-block.php';

17 of 22

More goodies

18 of 22

Suddenly things don’t look so bad!

— YOU, RIGHT NOW

19 of 22

Let’s make it editable

Step 4

20 of 22

What do we need to change?

  • Include wp.blocks.RichText
  • Add attributes props to the block
  • Register a handler for data changes
  • Pass props through the save() function

21 of 22

Code demo time.

22 of 22

Questions?