Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Think Outside the Block

Think Outside the Block

The block editor in WordPress (aka Gutenberg) offers a structured way of creating content through customizable “blocks”. But building custom blocks isn’t the only way to extend the editor; you can also register sidebars, enable custom toolbar buttons, and much more. In this talk, you’ll learn how to use these extension points to transform the block editor… without building a single block.

Chris Van Patten

February 07, 2020
Tweet

More Decks by Chris Van Patten

Other Decks in Technology

Transcript

  1. Blocks are the building blocks, but you don't have to

    build blocks to customise the block editor
  2. register_block_style( 'core/quote', [ 'name' => 'cool-design', 'label' => __( 'Cool

    Design' ), 'inline_style' => '' . '.wp-block-quote.is-style-cool-design {' . 'background: red;' . 'border: 3px solid blue;' . 'padding: 1rem;' . '}', ] );
  3. $colors = [ [ 'name' => 'Brand Red', 'slug' =>

    'brand-red', 'color' => '#B86251', ], ]; add_theme_support( 'editor-color-palette', $colors );
  4. $sizes = [ [ 'name' => 'Super Large', 'slug' =>

    'super-large', 'size' => 80, ], ]; add_theme_support( 'editor-font-sizes', $sizes );
  5. import { applyFormat } from '@wordpress/rich-text'; import { RichTextToolbarButton }

    from '@wordpress/editor'; const MyFormat = ( { isActive, onChange, value } ) => ( <RichTextToolbarButton icon="admin-customizer" isActive={ isActive } onClick={ () => onChange( applyFormat( value, { type: 'wcphx/fancy-text' } ) ) } title="Fancy Text" /> );
  6. import { registerFormatType } from '@wordpress/rich-text'; import MyFormat from './my-format';

    registerFormatType( 'wcphx/fancy-text', { className: 'wcphx-fancy-text', edit: MyFormat, tagName: 'span', title: 'Fancy Text', }, );
  7. import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-post'; import { registerPlugin

    } from '@wordpress/plugins'; const name = 'wcphx-sidebar'; const title = 'WordCamp Phoenix'; const icon = 'smiley';
  8. const Sidebar = () => ( <> <PluginSidebarMoreMenuItem icon={ icon

    } target={ name } > { title } </PluginSidebarMoreMenuItem>
  9. <PluginSidebar icon={ icon } name={ name } title={ title }

    > Hello Phoenix! </PluginSidebar> </> );
  10. import { createBlock } from '@wordpress/blocks'; import { Button }

    from '@wordpress/components'; import { dispatch } from '@wordpress/data'; import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; import { registerPlugin } from '@wordpress/plugins';
  11. <Button isPrimary onClick={ () => { const { editPost, insertBlocks,

    } = dispatch( 'core/editor' ); const block = createBlock( 'wcphx/demo' ); editPost( { title: 'WordCamp Phoenix!' } ); insertBlocks( block ); } } > Insert New Demo Block </Button>