Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to conditionally register/unregister patterns #307

Closed
bph opened this issue Sep 10, 2024 Discussed in #296 · 5 comments
Closed

How to conditionally register/unregister patterns #307

bph opened this issue Sep 10, 2024 Discussed in #296 · 5 comments
Assignees

Comments

@bph
Copy link
Collaborator

bph commented Sep 10, 2024

Discussed in #296

Originally posted by justintadlock August 28, 2024
When building a theme, I am sometimes building patterns that rely on a third-party block. In general, this works well enough with WordPress, giving a notice to the user that they need to install and activate the block.

image

However, not all theme authors want those patterns to appear without the block installed.

It's easy enough to only register patterns with a late init action and a conditional check. However, themers lose the ability to use the auto-registration via the /patterns folder.

Instead, we can conditionally unregister these without losing /patterns folder support. This would be a short tutorial with a reasonably small bit of code to explain the technique.

A code example is here: https://github.com/x3p0-dev/x3p0-ideas/blob/869d801bd7e11dccffb4b344b174c7b52ac89e52/src/Patterns.php#L134-L152

I'm not 100% sure this is worthy of a full tutorial, but not everything needs to be epic either. Sometimes these small techniques can be very helpful. This should definitely be sub-1,000 words.

@bph bph added the flow: needs writer the idea/pitch needs a writer label Sep 26, 2024
@justintadlock justintadlock self-assigned this Oct 3, 2024
@justintadlock
Copy link

I wrote a proof of concept for this as a snippet-style post: https://docs.google.com/document/d/1S4aWXZ-Q3GUklA82XAi4fq8ZdtJBseAYl9I_zeSAeVI/edit?usp=sharing

For more information on Snippets, see: #314

@bph bph removed the flow: needs writer the idea/pitch needs a writer label Oct 8, 2024
@justintadlock
Copy link

This can be reviewed now: https://docs.google.com/document/d/1S4aWXZ-Q3GUklA82XAi4fq8ZdtJBseAYl9I_zeSAeVI/edit?usp=sharing

I've also added this a draft in the backend to test what Snippet post types will look like: https://developer.wordpress.org/news/?post_type=snippets&p=4596&preview=true

@justintadlock
Copy link

Props to @ndiego and @ryanwelcher for 1st and 2nd reviews.

We still need to get a proper thumbnail for this before pushing live but looking to do so tomorrow.

@justintadlock
Copy link

Social copy:

Learn how to conditionally unregister block patterns with this handy snippet:

https://developer.wordpress.org/news/snippets/conditionally-unregister-patterns/

@jonathanbossenger
Copy link

Social scheduled for October 31st.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Published (Done)
Development

No branches or pull requests

3 participants