kaiseki / wp-context
Composable WordPress context filters (current user, post type, page template, content)
Requires
- php: ^8.2
- psr/container: ^1.1 || ^2.0
- thecodingmachine/safe: ^1.3 || ^2.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.1
- kaiseki/php-coding-standard: ^1.0
- maglnet/composer-require-checker: ^4.0
- php-stubs/wordpress-stubs: ^6.2
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.0
- roave/security-advisories: dev-latest
- roots/wordpress-core-installer: *
- roots/wordpress-no-content: @stable
- szepeviktor/phpstan-wordpress: ^2.0
- thecodingmachine/phpstan-safe-rule: ^1.4
This package is auto-updated.
Last update: 2026-06-02 23:46:57 UTC
README
Composable WordPress context filters (current user, post type, page template, content).
A context filter is any object implementing ContextFilterInterface —
__invoke(?WP_Post $post = null): bool. It answers a yes/no question about the current request
(or a given post): "is this a page?", "can the current user edit posts?", "does the content contain
this block?". ContextFilterPipeline ANDs several filters together (and is itself a filter, so
pipelines nest), which makes it easy to gate behavior on a combination of conditions.
Installation
composer require kaiseki/wp-context
Requires PHP 8.2 or newer.
Usage
use Kaiseki\WordPress\Context\Filter\ContextFilterPipeline; use Kaiseki\WordPress\Context\Filter\CurrentUserCan; use Kaiseki\WordPress\Context\Filter\IsPostType; $filter = new ContextFilterPipeline( new IsPostType('page'), new CurrentUserCan('edit_pages'), ); if ($filter($post)) { // the request is a 'page' AND the current user can edit pages }
Each filter is invokable on its own and accepts an optional WP_Post; when omitted it resolves the
current post / global context. ContextFilterPipeline returns false as soon as one filter fails.
Included filters
| Filter (and its negation) | True when |
|---|---|
ContextFilterPipeline |
every contained filter passes |
IsPostType / IsNotPostType |
the post matches the given post type |
IsFrontPage |
the post is the configured front page |
IsPageTemplate / IsNotPageTemplate |
the post uses the given page template |
IsPageTemplateFileName / IsNotPageTemplateFileName |
the post's page template matches the given file name |
IsUserLoggedIn / IsUserNotLoggedIn |
a user is logged in |
CurrentUserCan / CurrentUserCanNot |
the current user has the given capability |
CurrentUserHasRole / CurrentUserHasNotRole |
the current user has the given role |
CurrentUserHasEmail / CurrentUserHasNotEmail |
the current user has the given email |
CurrentUserIsAdmin / CurrentUserIsNotAdmin |
the current user is an administrator |
ContentContainsBlock / ContentDoesNotContainBlock |
the post content contains the given block |
ContentContainsRegex / ContentDoesNotContainRegex |
the post content matches the given pattern |
Write your own by implementing ContextFilterInterface:
use Kaiseki\WordPress\Context\Filter\ContextFilterInterface; use WP_Post; final class IsSticky implements ContextFilterInterface { public function __invoke(?WP_Post $post = null): bool { return $post !== null && is_sticky($post->ID); } }
Development
composer install
composer check # check-deps, cs-check, phpstan
License
MIT — see LICENSE.