Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Inspired by@WordPress Hooks (filters and actions) into modern PHP Hooks system

License

NotificationsYou must be signed in to change notification settings

simplemediacode/hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 theHooksInterface 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

Viacomposer:

composer require simplemediacode/hooks

Usage

Using the Static Facade

useSimplemediacode\Hooks\Hooks;// Add a filterHooks::addFilter('content',function($content) {returnstrtoupper($content);});// Apply a filter$content = Hooks::applyFilters('content','Hello World');// Returns "HELLO WORLD"// Add an actionHooks::addAction('save_post',function($postId) {// Do something when a post is savedecho"Post{$postId} was saved!";});// Execute an actionHooks::doAction('save_post',123);

Using Dependency Injection

useSimplemediacode\Hooks\HooksInterface;class MyClass{private ?HooksInterface$hooks;publicfunction__construct(        ?HooksInterface$hooks =null    ) {$this->hooks =$hooks;    }publicfunctionprocessContent(string$content):string    {// If hooks are available, filter the contentif ($this->hooks) {$content =$this->hooks->executeHook('content',$content);        }return$content;    }}

Extending with Custom Implementations

You can implement your own version ofHooksInterface to provide custom hook functionality:

$customHooks =newMyCustomHooksImplementation();Hooks::setInstance($customHooks);

Migration from WP_Hook

This package is a complete rewrite of the original WordPress hook system. Key differences:

  • RenamedWP_Hook toHook
  • 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 atCHANGELOG.md.

License

This library is released under the GPL-2.0 license. See the complete license in the bundledLICENSE file.


[8]ページ先頭

©2009-2025 Movatter.jp