Skip to content

How to conditionally register/unregister patterns #307

@bph

Description

@bph

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.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Published (Done)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions