WordPress / developer-blog-content

In this GitHub space, WordPress team coordinate content to be published on the Developer Blog. Discussion and montly meetings (first Thu) in WP Slack #core-dev-blog
38 stars 4 forks source link

How to conditionally register/unregister patterns #307

Open bph opened 3 weeks ago

bph commented 3 weeks ago

Discussed in https://github.com/WordPress/developer-blog-content/discussions/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](https://github.com/user-attachments/assets/1e537a9f-8d79-4eec-980f-76ede4acd3ca) 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.