SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Creating a custom block pattern plugin
Jeff McNear
https://plasterdog.com
https://webprosmeetup.org
jeff@plasterdog.com
847/849-7060
The Gutenberg editor is here to stay
• Block based editing is efficient and user friendly
• You can do most things with core blocks
• Structural blocks facilitate complex layouts
• The interface is consistently improving
• We can pick and choose which elements to use
You can build almost anything with the core
set of blocks
A quick (and opinionated) pass
through the default set of
Gutenberg blocks:
Text Group
• Most are related to typography
• Classic reverts to the old style
• Code is handy for some of us
• Tables are not for layout
Media Group
• HTML5 video & audio players
• Sophisticated image controls
• Easily embed files
Design Group
• Pre formatted buttons
• Container blocks
• Superior control over spacing
• Promotes the nesting of blocks
Widgets Group
• Kind of a hodge podge
• Custom HTML for in-line editing
• Some plugins that generate
shortcodes will not have a block
• Common queries and feeds
Theme Group
• Header elements
• Query loops
• Query loop components
• Full site editing features that can
be used in places where the
block editor is active
• More query loop components
• Archive components
• Login form
Embed Group
While there is justification for
adding supplemental blocks
and/or creating a custom block –
usually this will be an over-
complication
Many times, when you think you
would need a custom block, what
you really need is a configuration
of nested blocks
It only makes sense to make provisions for saving
block configurations for reuse in other places…
Reusable blocks are like Illustrator symbols
First assemble the group of blocks
Then save the group as a reusable block
Once saved there will be a ”reusable” tab
For some reason there is no link to “/wp-admin/edit.php?post_type=wp_block” Except for in this tab…
Via the “manage” link the reusable block can be edited like any
other post type
You can import & export reusable block JSON files
Each instance of a reusable block will reflect any changes made
in the “parent”
But any instance of a
reusable block can be
converted back to a
collection of “regular”
blocks
So that sort of works…
There has got to be a better way to insert preformatted sections
that are intended to be modified and edited
Block patterns are assembled groups of blocks that are
intended to be editable
There are dozens of patterns within 9 existing categories
Block patterns are for all intents and purposes page (or post)
template parts that you can use at will in any part of the site
that uses the block editor
More block patterns can be found at: https://wordpress.org/patterns/
But using these supplemental patterns is a little clunky –
• You need to copy and paste the pattern
• To effectively paste you need to be in code view
… and worst of all they won’t be saved in the block insertor
Unlike with reusable blocks, there has been no provision
to edit, export or import block patterns
There are a couple of block pattern builder plugins, but they
are not in wide use and do not have an export function either
But creating a bespoke plugin creating custom patterns is not
too difficult.
<?php
/**
* Plugin Name: Bill Murray Pattern
* Description: Bill Murray pattern demo
* Version: 1.05
* Author: Jeff McNear
*/
/**
* Register Custom Block Pattern Styles
*/
Name and describe the plugin
add_action('init', function() {
remove_theme_support('core-block-patterns');
});
This block of code will remove most of the core block
patterns
add_action('init', function() {
if (!function_exists('unregister_block_pattern'))
{
return;
}
unregister_block_pattern('core/two-buttons');
unregister_block_pattern('core/three-buttons');
});
This block of code will remove two specific block
patterns: two-buttons & three-buttons
There are some things that can be done to clean up the interface
(usually)
add_action('init', function() {
if (!function_exists('unregister_block_pattern_category'))
{ return; } unregister_block_pattern_category('buttons');
});
This block of code will remove the block category
“buttons”
Note:
Themes are increasingly including custom patterns and block pattern categories – this means that the snippets above
will only work when there is no contradiction in the theme
add_action('init', function() {
remove_theme_support('core-block-patterns');
});
This block of code will (usually) remove all of
the core block patterns
Organizing custom patterns in a custom category will make
things easier
register_block_pattern_category(
'bill',
array( 'label' => __( 'Bill Murray', 'bmp' ) )
);
This block of code will
create a new category
to supplement the
core group of
categories
Once these decisions are made it is time to define the new
patterns
add_action('init', function() {
if (!function_exists('register_block_pattern')) {
return;
}
Opens the function registering new
block patterns
}); Closes the function
The definitions for the new patterns are placed in this section
Each pattern is defined by five elements:
// starts the pattern
register_block_pattern(‘bmp/bill-murray-bio', [
'title' => __(Bill Murray Bio', ‘bmp'),
'keywords' => [Bill Murray, bio'],
'categories' => ['featured’ , ‘bill’],
'viewportWidth' => 1000,
'content' => " ",
]);
// ends the pattern
Register the pattern
Name the pattern
Relevant keywords
Relevant categories
Viewport width
Content*
*This is the “tricky” part
Ends the pattern
The most straight forward way to compose the
content section is to first build the assembled group of blocks
in the Gutenberg editor.
Note: it is best to draw
images from offsite sources
such as:
https://picsum.photos
https://holderjs.com
https://fillmurray.com
https://placekeanu.com
https://placekitten.com
https://placebear.com
https://placedog.net/
Switch to the code editor view and select the code:
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"66.66%"} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:heading -->
<h2>A new Pattern</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>some descriptive text</p>
<!-- /wp:paragraph -->
<!-- wp:social-links -->
<ul class="wp-block-social-links"><!-- wp:social-link {"service":"facebook"} /-->
<!-- wp:social-link {"service":"youtube"} /-->
<!-- wp:social-link {"service":"twitter"} /-->
<!-- wp:social-link {"service":"linkedin"} /--></ul>
<!-- /wp:social-links --></div>
<!-- /wp:column -->
<!-- wp:column {"width":"33.33%"} -->
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.fillmurray.com/g/400/400" alt=""/></figure>
<!-- /wp:image --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
HTML markup contains strings
which need to be ”escaped” to
work properly here
This tool will make sure the escapes are correctly written:
https://onlinestringtools.com/escape-string
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"66.66%"} -->
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:heading -->
<h2>A new Pattern</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>some descriptive text</p>
<!-- /wp:paragraph -->
<!-- wp:social-links -->
<ul class="wp-block-social-links"><!-- wp:social-link {"service":"facebook"} /-->
<!-- wp:social-link {"service":"youtube"} /-->
<!-- wp:social-link {"service":"twitter"} /-->
<!-- wp:social-link {"service":"linkedin"} /--></ul>
<!-- /wp:social-links --></div>
<!-- /wp:column -->
<!-- wp:column {"width":"33.33%"} -->
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.fillmurray.com/g/400/400" alt=""/></figure>
<!-- /wp:image --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->n
… and here is the result!
Once your plugin is installed and
activated you will have access to
your custom block pattern
Like any other block pattern once inserted each individual
block will remain completely editable
(in that instance)
Then you can change out the contents of each block within the
pattern for each specific use case
Questions?

Weitere ähnliche Inhalte

Ähnlich wie Build and save your own Gutenberg Block Patterns

Web components - An Introduction
Web components - An IntroductionWeb components - An Introduction
Web components - An Introductioncherukumilli2
 
"Umbraco MVC - a journey of discovery" - Lotte Pitcher
"Umbraco MVC - a journey of discovery" - Lotte Pitcher"Umbraco MVC - a journey of discovery" - Lotte Pitcher
"Umbraco MVC - a journey of discovery" - Lotte Pitcherlottepitcher
 
BEM it! Introduction to BEM
BEM it! Introduction to BEMBEM it! Introduction to BEM
BEM it! Introduction to BEMVarya Stepanova
 
Improving your responsive workflow with style guides
Improving your responsive workflow with style guidesImproving your responsive workflow with style guides
Improving your responsive workflow with style guidesLuke Brooker
 
Introduction to-concrete-5
Introduction to-concrete-5Introduction to-concrete-5
Introduction to-concrete-5Ketan Raval
 
Introduction to-concrete-5
Introduction to-concrete-5Introduction to-concrete-5
Introduction to-concrete-5ketanraval
 
Highly Maintainable, Efficient, and Optimized CSS
Highly Maintainable, Efficient, and Optimized CSSHighly Maintainable, Efficient, and Optimized CSS
Highly Maintainable, Efficient, and Optimized CSSZoe Gillenwater
 
Joomla Beginner Template Presentation
Joomla Beginner Template PresentationJoomla Beginner Template Presentation
Joomla Beginner Template Presentationalledia
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013   shadow dom - mihai bîrsanIasi code camp 12 october 2013   shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsanCodecamp Romania
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for DrupalWingston
 
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...Anson Han
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extensionBun Danny
 
Expanding XPages with Bootstrap Plugins for Ultimate Usability
Expanding XPages with Bootstrap Plugins for Ultimate UsabilityExpanding XPages with Bootstrap Plugins for Ultimate Usability
Expanding XPages with Bootstrap Plugins for Ultimate UsabilityTeamstudio
 
Maintainable Frontend Development with BEM
Maintainable Frontend Development with BEMMaintainable Frontend Development with BEM
Maintainable Frontend Development with BEMVarya Stepanova
 
Introduction to BOOTSTRAP
Introduction to BOOTSTRAPIntroduction to BOOTSTRAP
Introduction to BOOTSTRAPJeanie Arnoco
 
The Joy of Subforms with Randy Carey
The Joy of Subforms with Randy CareyThe Joy of Subforms with Randy Carey
The Joy of Subforms with Randy Careyjdaychi
 

Ähnlich wie Build and save your own Gutenberg Block Patterns (20)

Web components - An Introduction
Web components - An IntroductionWeb components - An Introduction
Web components - An Introduction
 
Fewd week2 slides
Fewd week2 slidesFewd week2 slides
Fewd week2 slides
 
"Umbraco MVC - a journey of discovery" - Lotte Pitcher
"Umbraco MVC - a journey of discovery" - Lotte Pitcher"Umbraco MVC - a journey of discovery" - Lotte Pitcher
"Umbraco MVC - a journey of discovery" - Lotte Pitcher
 
BEM it! Introduction to BEM
BEM it! Introduction to BEMBEM it! Introduction to BEM
BEM it! Introduction to BEM
 
Improving your responsive workflow with style guides
Improving your responsive workflow with style guidesImproving your responsive workflow with style guides
Improving your responsive workflow with style guides
 
Introduction to-concrete-5
Introduction to-concrete-5Introduction to-concrete-5
Introduction to-concrete-5
 
Introduction to-concrete-5
Introduction to-concrete-5Introduction to-concrete-5
Introduction to-concrete-5
 
Atomic design
Atomic designAtomic design
Atomic design
 
Highly Maintainable, Efficient, and Optimized CSS
Highly Maintainable, Efficient, and Optimized CSSHighly Maintainable, Efficient, and Optimized CSS
Highly Maintainable, Efficient, and Optimized CSS
 
Joomla Beginner Template Presentation
Joomla Beginner Template PresentationJoomla Beginner Template Presentation
Joomla Beginner Template Presentation
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013   shadow dom - mihai bîrsanIasi code camp 12 october 2013   shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
 
Responsive web design
Responsive web designResponsive web design
Responsive web design
 
6 Special Howtos for Drupal
6 Special Howtos for Drupal6 Special Howtos for Drupal
6 Special Howtos for Drupal
 
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...
Drupaldelphia 2013 Presentation- Making Your Site more Friendly to Search Eng...
 
18. images in symfony 4
18. images in symfony 418. images in symfony 4
18. images in symfony 4
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extension
 
Expanding XPages with Bootstrap Plugins for Ultimate Usability
Expanding XPages with Bootstrap Plugins for Ultimate UsabilityExpanding XPages with Bootstrap Plugins for Ultimate Usability
Expanding XPages with Bootstrap Plugins for Ultimate Usability
 
Maintainable Frontend Development with BEM
Maintainable Frontend Development with BEMMaintainable Frontend Development with BEM
Maintainable Frontend Development with BEM
 
Introduction to BOOTSTRAP
Introduction to BOOTSTRAPIntroduction to BOOTSTRAP
Introduction to BOOTSTRAP
 
The Joy of Subforms with Randy Carey
The Joy of Subforms with Randy CareyThe Joy of Subforms with Randy Carey
The Joy of Subforms with Randy Carey
 

Mehr von Plasterdog Web Design (6)

Wordpress overview
Wordpress overviewWordpress overview
Wordpress overview
 
Pantheon basics
Pantheon basicsPantheon basics
Pantheon basics
 
Wordpress Security & Hardening Steps
Wordpress Security & Hardening StepsWordpress Security & Hardening Steps
Wordpress Security & Hardening Steps
 
Basic wordpress editing
Basic wordpress editingBasic wordpress editing
Basic wordpress editing
 
Youtube Basics
Youtube BasicsYoutube Basics
Youtube Basics
 
Wordpress multisite
Wordpress multisiteWordpress multisite
Wordpress multisite
 

Kürzlich hochgeladen

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Kürzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Build and save your own Gutenberg Block Patterns

  • 1. Creating a custom block pattern plugin Jeff McNear https://plasterdog.com https://webprosmeetup.org jeff@plasterdog.com 847/849-7060
  • 2. The Gutenberg editor is here to stay • Block based editing is efficient and user friendly • You can do most things with core blocks • Structural blocks facilitate complex layouts • The interface is consistently improving • We can pick and choose which elements to use
  • 3. You can build almost anything with the core set of blocks
  • 4. A quick (and opinionated) pass through the default set of Gutenberg blocks:
  • 5. Text Group • Most are related to typography • Classic reverts to the old style • Code is handy for some of us • Tables are not for layout
  • 6. Media Group • HTML5 video & audio players • Sophisticated image controls • Easily embed files
  • 7. Design Group • Pre formatted buttons • Container blocks • Superior control over spacing • Promotes the nesting of blocks
  • 8. Widgets Group • Kind of a hodge podge • Custom HTML for in-line editing • Some plugins that generate shortcodes will not have a block • Common queries and feeds
  • 9. Theme Group • Header elements • Query loops • Query loop components • Full site editing features that can be used in places where the block editor is active
  • 10. • More query loop components • Archive components • Login form
  • 12. While there is justification for adding supplemental blocks and/or creating a custom block – usually this will be an over- complication Many times, when you think you would need a custom block, what you really need is a configuration of nested blocks
  • 13. It only makes sense to make provisions for saving block configurations for reuse in other places…
  • 14. Reusable blocks are like Illustrator symbols First assemble the group of blocks Then save the group as a reusable block
  • 15. Once saved there will be a ”reusable” tab For some reason there is no link to “/wp-admin/edit.php?post_type=wp_block” Except for in this tab…
  • 16. Via the “manage” link the reusable block can be edited like any other post type
  • 17. You can import & export reusable block JSON files
  • 18. Each instance of a reusable block will reflect any changes made in the “parent” But any instance of a reusable block can be converted back to a collection of “regular” blocks
  • 19. So that sort of works…
  • 20. There has got to be a better way to insert preformatted sections that are intended to be modified and edited
  • 21. Block patterns are assembled groups of blocks that are intended to be editable There are dozens of patterns within 9 existing categories
  • 22. Block patterns are for all intents and purposes page (or post) template parts that you can use at will in any part of the site that uses the block editor
  • 23. More block patterns can be found at: https://wordpress.org/patterns/
  • 24. But using these supplemental patterns is a little clunky – • You need to copy and paste the pattern • To effectively paste you need to be in code view
  • 25. … and worst of all they won’t be saved in the block insertor
  • 26. Unlike with reusable blocks, there has been no provision to edit, export or import block patterns
  • 27. There are a couple of block pattern builder plugins, but they are not in wide use and do not have an export function either
  • 28. But creating a bespoke plugin creating custom patterns is not too difficult. <?php /** * Plugin Name: Bill Murray Pattern * Description: Bill Murray pattern demo * Version: 1.05 * Author: Jeff McNear */ /** * Register Custom Block Pattern Styles */ Name and describe the plugin
  • 29. add_action('init', function() { remove_theme_support('core-block-patterns'); }); This block of code will remove most of the core block patterns add_action('init', function() { if (!function_exists('unregister_block_pattern')) { return; } unregister_block_pattern('core/two-buttons'); unregister_block_pattern('core/three-buttons'); }); This block of code will remove two specific block patterns: two-buttons & three-buttons There are some things that can be done to clean up the interface (usually)
  • 30. add_action('init', function() { if (!function_exists('unregister_block_pattern_category')) { return; } unregister_block_pattern_category('buttons'); }); This block of code will remove the block category “buttons” Note: Themes are increasingly including custom patterns and block pattern categories – this means that the snippets above will only work when there is no contradiction in the theme add_action('init', function() { remove_theme_support('core-block-patterns'); }); This block of code will (usually) remove all of the core block patterns
  • 31. Organizing custom patterns in a custom category will make things easier register_block_pattern_category( 'bill', array( 'label' => __( 'Bill Murray', 'bmp' ) ) ); This block of code will create a new category to supplement the core group of categories
  • 32. Once these decisions are made it is time to define the new patterns add_action('init', function() { if (!function_exists('register_block_pattern')) { return; } Opens the function registering new block patterns }); Closes the function The definitions for the new patterns are placed in this section
  • 33. Each pattern is defined by five elements: // starts the pattern register_block_pattern(‘bmp/bill-murray-bio', [ 'title' => __(Bill Murray Bio', ‘bmp'), 'keywords' => [Bill Murray, bio'], 'categories' => ['featured’ , ‘bill’], 'viewportWidth' => 1000, 'content' => " ", ]); // ends the pattern Register the pattern Name the pattern Relevant keywords Relevant categories Viewport width Content* *This is the “tricky” part Ends the pattern
  • 34. The most straight forward way to compose the content section is to first build the assembled group of blocks in the Gutenberg editor. Note: it is best to draw images from offsite sources such as: https://picsum.photos https://holderjs.com https://fillmurray.com https://placekeanu.com https://placekitten.com https://placebear.com https://placedog.net/
  • 35. Switch to the code editor view and select the code: <!-- wp:columns --> <div class="wp-block-columns"><!-- wp:column {"width":"66.66%"} --> <div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:heading --> <h2>A new Pattern</h2> <!-- /wp:heading --> <!-- wp:paragraph --> <p>some descriptive text</p> <!-- /wp:paragraph --> <!-- wp:social-links --> <ul class="wp-block-social-links"><!-- wp:social-link {"service":"facebook"} /--> <!-- wp:social-link {"service":"youtube"} /--> <!-- wp:social-link {"service":"twitter"} /--> <!-- wp:social-link {"service":"linkedin"} /--></ul> <!-- /wp:social-links --></div> <!-- /wp:column --> <!-- wp:column {"width":"33.33%"} --> <div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:image {"sizeSlug":"large"} --> <figure class="wp-block-image size-large"><img src="https://www.fillmurray.com/g/400/400" alt=""/></figure> <!-- /wp:image --></div> <!-- /wp:column --></div> <!-- /wp:columns -->
  • 36. HTML markup contains strings which need to be ”escaped” to work properly here
  • 37. This tool will make sure the escapes are correctly written: https://onlinestringtools.com/escape-string
  • 38. <!-- wp:columns --> <div class="wp-block-columns"><!-- wp:column {"width":"66.66%"} --> <div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:heading --> <h2>A new Pattern</h2> <!-- /wp:heading --> <!-- wp:paragraph --> <p>some descriptive text</p> <!-- /wp:paragraph --> <!-- wp:social-links --> <ul class="wp-block-social-links"><!-- wp:social-link {"service":"facebook"} /--> <!-- wp:social-link {"service":"youtube"} /--> <!-- wp:social-link {"service":"twitter"} /--> <!-- wp:social-link {"service":"linkedin"} /--></ul> <!-- /wp:social-links --></div> <!-- /wp:column --> <!-- wp:column {"width":"33.33%"} --> <div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:image {"sizeSlug":"large"} --> <figure class="wp-block-image size-large"><img src="https://www.fillmurray.com/g/400/400" alt=""/></figure> <!-- /wp:image --></div> <!-- /wp:column --></div> <!-- /wp:columns -->n … and here is the result!
  • 39. Once your plugin is installed and activated you will have access to your custom block pattern
  • 40. Like any other block pattern once inserted each individual block will remain completely editable (in that instance)
  • 41. Then you can change out the contents of each block within the pattern for each specific use case