https://github.com/simplemediacode/hooks
Decoupled @WordPress Hooks (filters and actions)
https://github.com/simplemediacode/hooks
composer composer-package hook hook-functions hooking hooking-library hooks hooks-api php php7 php8 wordpress wordpress-development wordpress-php-library
Last synced: 3 months ago
JSON representation
Decoupled @WordPress Hooks (filters and actions)
- Host: GitHub
- URL: https://github.com/simplemediacode/hooks
- Owner: simplemediacode
- License: gpl-2.0
- Created: 2020-12-28T22:36:02.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-31T17:11:38.000Z (3 months ago)
- Last Synced: 2025-03-31T17:46:20.032Z (3 months ago)
- Topics: composer, composer-package, hook, hook-functions, hooking, hooking-library, hooks, hooks-api, php, php7, php8, wordpress, wordpress-development, wordpress-php-library
- Language: PHP
- Homepage: https://republa.com
- Size: 47.9 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Modern PHP Hooks System
A lightweight, dependency-free implementation of an action and filter hook system inspired by WordPress hooks but designed for modern PHP applications.
## Features
- **Modern PHP Support**: Fully typed, supports PHP 8.0+
- **Dependency Injection Ready**: Use the `HooksInterface` in your classes
- **Static Facade**: Convenient static methods for quick integration
- **No Dependencies**: Lightweight implementation with zero dependencies
- **WordPress Inspired**: Familiar API if you're coming from WordPress## Installation
Via **composer**:
```bash
composer require simplemediacode/hooks
```## Usage
### Using the Static Facade
```php
use Simplemediacode\Hooks\Hooks;// Add a filter
Hooks::addFilter('content', function($content) {
return strtoupper($content);
});// Apply a filter
$content = Hooks::applyFilters('content', 'Hello World'); // Returns "HELLO WORLD"// Add an action
Hooks::addAction('save_post', function($postId) {
// Do something when a post is saved
echo "Post {$postId} was saved!";
});// Execute an action
Hooks::doAction('save_post', 123);
```### Using Dependency Injection
```php
use Simplemediacode\Hooks\HooksInterface;class MyClass
{
private ?HooksInterface $hooks;public function __construct(
?HooksInterface $hooks = null
) {
$this->hooks = $hooks;
}
public function processContent(string $content): string
{
// If hooks are available, filter the content
if ($this->hooks) {
$content = $this->hooks->executeHook('content', $content);
}
return $content;
}
}
```### Extending with Custom Implementations
You can implement your own version of `HooksInterface` to provide custom hook functionality:
```php
$customHooks = new MyCustomHooksImplementation();
Hooks::setInstance($customHooks);
```## Migration from WP_Hook
This package is a complete rewrite of the original WordPress hook system. Key differences:
- Renamed `WP_Hook` to `Hook`
- Introduced proper interfaces for better type safety
- Added dependency injection support
- Replaced global variables with proper class properties
- Improved naming conventions and method signatures## Changelog
Read at [CHANGELOG.md](./CHANGELOG.md).
## License
This library is released under the GPL-2.0 license. See the complete license in the bundled [LICENSE](./LICENSE) file.