— William Shakespeare
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
Block Filters allow you to extend or
change the functionality of a block.
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
Hooks

Actions

Filters

@_pbrocks #codeisalchemy WordCamp Lancaster 2019
To modify the behavior of existing blocks,
WordPress exposes several APIs.

@_pbrocks #codeisalchemy WordCamp Lancaster 2019
Enable you to provides alternative styles to existing blocks.
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
These filters can extend the block settings:
blocks.registerBlockTypeblocks.getSaveElementblocks.getSaveContent.extraPropsblocks.getBlockDefaultClassNameblocks.switchToBlockType.transformedBlockblocks.getBlockAttributes editor.BlockEditeditor.BlockListBlock@_pbrocks #codeisalchemy WordCamp Lancaster 2019
These filters can control the use of blocks in your site:
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
or How to Extend a Block
@_pbrocks #codeisalchemy WordCamp Lancaster 2019

@_pbrocks #codeisalchemy WordCamp Lancaster 2019

@_pbrocks #codeisalchemy WordCamp Lancaster 2019
| Add UI to add core image block style | |
| wp.blocks.registerBlockStyle( 'core/image', { | |
| name: 'default', | |
| label: 'Default' | |
| } ); | |
| wp.blocks.registerBlockStyle( 'core/image', { | |
| name: 'phader', | |
| label: 'Philly Phader' | |
| } ); |

@_pbrocks #codeisalchemy WordCamp Lancaster 2019
This method of block customization keeps functionality where it belongs. Rather than adding to the growing list of blocks you may access, we can simply apply the style where it is needed — and where the user expects to find it.
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
| <?php | |
| /** | |
| * Adding a block category creates a Panel | |
| */ | |
| function create_wclancpa_2019_panel( $categories, $post ) { | |
| return array_merge( | |
| $categories, | |
| array( | |
| array( | |
| 'slug' => 'wclancpa-2019', | |
| 'title' => __( 'WC Lancaster 2019 Panel', 'wclancpa-2019' ), | |
| ), | |
| ) | |
| ); | |
| } | |
| add_filter( 'block_categories', 'create_wclancpa_2019_panel', 10, 2 ); |
@_pbrocks #codeisalchemy WordCamp Lancaster 2019

@_pbrocks #codeisalchemy WordCamp Lancaster 2019
| // svg code | |
| import lancpaIcon from './lancpaIcon'; | |
| // alter the icon slot | |
| wp.blocks.updateCategory( 'wclancpa-2019', { | |
| icon: lancpaIcon | |
| } ); |
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
Moving beyond experiments
With the advent of the new block editor, our shared development processes and best practices are shifting.
It's the Wild, Wild, West all over again.
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
Why use an HOC?
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
HOCs are a React JS construct that refer to a function that takes a component and returns a new component.
@_pbrocks #codeisalchemy WordCamp Lancaster 2019
| const { createHigherOrderComponent } = wp.compose; | |
| const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => { | |
| return ( props ) => { | |
| return <BlockListBlock { ...props } className={ "block-" + props.clientId } />; | |
| }; | |
| }, 'withClientIdClassName' ); | |
| wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName ); |
Source: The WP Developer Handbook
@_pbrocks #codeisalchemy WordCamp Lancaster 2019

| <?php | |
| defined( 'ABSPATH' ) || die( 'File cannot be accessed directly' ); | |
| function built_with_php_init() { | |
| // Register our block editor script. | |
| wp_enqueue_script( | |
| 'built-with-php', | |
| plugins_url( 'block.js', __FILE__ ), | |
| array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' ) | |
| ); | |
| register_block_type( | |
| 'wclancpa-2019/built-with-php', | |
| array( | |
| 'attributes' => | |
| array( | |
| 'value_one' => array( | |
| 'type' => 'string', | |
| 'default' => 'A simple text box', | |
| ), | |
| 'value_two' => array( | |
| 'type' => 'string', | |
| 'default' => 'Ain\'t this purdy?', | |
| ), | |
| 'value_three' => array( | |
| 'type' => 'string', | |
| 'default' => 'This will be more of a description.... And I could go on and on...', | |
| ), | |
| ), | |
| // The script name we gave in the wp_register_script() call. | |
| 'editor_script' => 'built-with-php', | |
| 'render_callback' => 'built_with_php_render', | |
| ) | |
| ); | |
| add_shortcode( 'built-with-php-shortcode', 'built_with_php_render' ); | |
| } | |
| add_action( 'init', 'built_with_php_init' ); | |
| function built_with_php_render( $attributes ) { | |
| $return = '<h2 style="color:salmon;">' . ( print_r( $attributes['value_one'], true ) ?: 'value_one not defined' ) . '</h2>'; | |
| $return .= '<h4>' . print_r( $attributes['value_two'], true ) . '</h4>'; | |
| $return .= '<p>' . print_r( $attributes['value_three'], true ) . '</p>'; | |
| return $return; | |
| } |
| <?php | |
| function my_restrict_post_filter( $use_block_editor, $post ) { | |
| $author = get_userdata( $post->post_author ); | |
| if ( 'pento' === $author->user_login ) { | |
| return (bool) random_int( 0, 1 ); | |
| } | |
| return $use_block_editor; | |
| } | |
| add_filter( 'use_block_editor_for_post', 'my_restrict_post_filter', 10, 2 ); | |
| function my_restrict_post_type_filter( $use_block_editor, $post_type ) { | |
| if ( 'my_mystical_post_type' === $post_type ) { | |
| return false; | |
| } | |
| return $use_block_editor; | |
| } | |
| add_filter( 'use_block_editor_for_post_type', 'my_restrict_post_type_filter', 10, 2 ); | |
| // Block Filter reference | |
| // https://wordpress.stackexchange.com/questions/308021/add-custom-class-to-core-blocks-in-gutenberg?answertab=active#tab-top | |


| <?php | |
| /** | |
| * Our combined block and shortcode renderer. | |
| * | |
| * We're not using sophisticated php, so if using the shortcode we'd need to specify all three values. | |
| * | |
| * @param array $attributes The attributes set on the block or shortcode. | |
| */ | |
| function acf_built_with_php_render( $attributes ) { | |
| $return = '<h2 style="color:salmon;">' . ( print_r( $attributes['value_one'], true ) ?: 'value_one not defined' ) . '</h2>'; | |
| $return .= '<h4>' . print_r( get_field( 'the_title' ), true ) . '</h4>'; | |
| // $return .= '<h4>' . ( print_r( $attributes['value_two'], true ) ? print_r( get_field( 'the_title' ), true ) : print_r( $attributes['value_two'], true ) ), true ) . '</h4>'; | |
| $return .= '<p>' . print_r( $attributes['value_three'], true ) . '</p>'; | |
| $return .= '<h4>' . get_field( 'the_title' ) . '</h4>'; | |
| return $return; | |
| } |

@_pbrocks #codeisalchemy