{"id":19144376,"url":"https://github.com/piotrpress/wordpress-hooks","last_synced_at":"2025-08-21T04:31:18.683Z","repository":{"id":47373212,"uuid":"321300072","full_name":"PiotrPress/wordpress-hooks","owner":"PiotrPress","description":"The library allows using PHP Attributes (introduced in PHP version 8) to automagically add WordPress Hooks (Filters and Actions) to objects' methods.","archived":false,"fork":false,"pushed_at":"2024-09-16T12:06:59.000Z","size":32,"stargazers_count":34,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"5.x","last_synced_at":"2024-10-17T13:23:34.697Z","etag":null,"topics":["actions","attributes","filters","hook","hooks","php-attibutes","php8","wordpress","wordpress-composer","wordpress-hooks","wordpress-library","wordpress-oop","wordpress-oop-php","wordpress-package","wordpress-plugin","wp","wp-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PiotrPress.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-14T09:39:38.000Z","updated_at":"2024-09-16T16:15:08.000Z","dependencies_parsed_at":"2022-09-10T11:31:49.547Z","dependency_job_id":"d8389fd0-cd68-453d-b206-b19b78d133ca","html_url":"https://github.com/PiotrPress/wordpress-hooks","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"8f41c38a6394f21692bf5ca2ff8b48b0404d1142"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PiotrPress%2Fwordpress-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PiotrPress%2Fwordpress-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PiotrPress%2Fwordpress-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PiotrPress%2Fwordpress-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PiotrPress","download_url":"https://codeload.github.com/PiotrPress/wordpress-hooks/tar.gz/refs/heads/5.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230487844,"owners_count":18233865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actions","attributes","filters","hook","hooks","php-attibutes","php8","wordpress","wordpress-composer","wordpress-hooks","wordpress-library","wordpress-oop","wordpress-oop-php","wordpress-package","wordpress-plugin","wp","wp-plugin"],"created_at":"2024-11-09T07:34:41.870Z","updated_at":"2024-12-19T19:09:14.435Z","avatar_url":"https://github.com/PiotrPress.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WordPress Hooks\n\nThis library uses [PHP Attributes](https://www.php.net/manual/en/language.attributes.overview.php) (introduced in PHP version `8.0`) to automagically add/remove [WordPress Hooks](https://developer.wordpress.org/plugins/hooks/) ([Filters](https://codex.wordpress.org/Plugin_API/Filter_Reference) and [Actions](https://codex.wordpress.org/Plugin_API/Action_Reference)) to/from functions and methods.\n\n**Note:** The library supports PHP \u003e= `7.4` version.\n\n## Installation\n\n```console\n$ composer require piotrpress/wordpress-hooks\n```\n\n### Load\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n```\n\n## Usage\n\n### Attributes\n\n```php\n#[ Action( string $name, int $priority = 10 ) ]\n#[ Filter( string $name, int $priority = 10 ) ]\n```\n\n### Functions\n\n```php\nHooks::add( object $object = null, string $callback = '', PiotrPress\\CacherInterface $cache = null ) : void\nHooks::remove( object $object = null, string $callback = '', PiotrPress\\CacherInterface $cache = null ) : void\n```\n\n## Examples\n\n### Hooks::add/remove( $object )\n\nIf `object` argument is passed and `callback` is omitted, then all hooks from object are added or removed.\n\n```php\nuse PiotrPress\\WordPress\\Hooks;\nuse PiotrPress\\WordPress\\Hooks\\Action;\nuse PiotrPress\\WordPress\\Hooks\\Filter;\n\nclass Example {\n    public function add_hooks() {\n        Hooks::add( $this );\n    }\n\n    #[ Action( 'init' ) ]\n    public function example_init() : void {\n        // do something\n    }\n\n    #[ Filter( 'the_title', 1 ) ]\n    public function example_the_title( string $post_title, int $post_id ) : string {\n        // do something\n    }\n}\n\n$example = new Example();\n$example-\u003eadd_hooks();\n\nHooks::remove( $example );\n```\n\nThis is an equivalent to:\n\n```php\n$example = new Example();\n\nadd_action( 'init', [ $example, 'example_init' ] );\nadd_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );\n\nremove_action( 'init', [ $example, 'example_init' ] );\nremove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );\n```\n\n**Note:** `Hooks::add/remove()` methods can be called from the method, or even outside the object.\n\n### Hooks::add/remove( $object, $callback )\n\nIf `object` and `callback` arguments are passed, then only hooks for this method are added or removed.\n\n```php\nuse PiotrPress\\WordPress\\Hooks;\nuse PiotrPress\\WordPress\\Hooks\\Action;\nuse PiotrPress\\WordPress\\Hooks\\Filter;\n\nclass Example {\n    public function add_hooks() {\n        Hooks::add( $this, 'example_init' );\n        Hooks::add( $this, 'example_the_title' );\n    }\n\n    #[ Action( 'init' ) ]\n    public function example_init() : void {\n        // do something\n    }\n\n    #[ Filter( 'the_title', 1 ) ]\n    public function example_the_title( string $post_title, int $post_id ) : string {\n        // do something\n    }\n}\n\n$example = new Example();\n$example-\u003eadd_hooks();\n\nHooks::remove( $example, 'example_init' );\nHooks::remove( $example, 'example_the_title' );\n```\n\nThis is an equivalent to:\n\n```php\n$example = new Example();\n\nadd_action( 'init', [ $example, 'example_init' ] );\nadd_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );\n\nremove_action( 'init', [ $example, 'example_init' ] );\nremove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );\n```\n\n### Hooks::add/remove( callback: $callback )\n\nIf `object` argument is omitted and `callback` is passed, then only hooks for this function are added or removed.\n\n```php\nuse PiotrPress\\WordPress\\Hooks;\nuse PiotrPress\\WordPress\\Hooks\\Action;\nuse PiotrPress\\WordPress\\Hooks\\Filter;\n\n#[ Action( 'init' ) ]\npublic function example_init() : void {\n    // do something\n}\n\n#[ Filter( 'the_title', 1 ) ]\npublic function example_the_title( string $post_title, int $post_id ) : string {\n    // do something\n}\n\nHooks::add( callback: 'example_init' );\nHooks::add( callback: 'example_the_title' );\n\nHooks::remove( callback: 'example_init' );\nHooks::remove( callback: 'example_the_title' );\n```\n\nThis is an equivalent to:\n\n```php\nadd_action( 'init', 'example_init' );\nadd_filter( 'the_title', 'example_the_title', 1, 2 );\n\nremove_action( 'init', 'example_init' );\nremove_filter( 'the_title', 'example_the_title', 1, 2 );\n```\n\n## Cache\n\nOptionally, you can pass a cache object, which must implement [PiotrPress\\CacherInterface](https://github.com/PiotrPress/cacher/blob/master/src/CacherInterface.php) interface, as a third `cache` argument to `Hooks::add/remove()` methods.\n\nThis will cache the result of `Hooks::get()` method, which provides a list of hooks for a given object, method or function using [Reflection API](https://www.php.net/manual/en/book.reflection.php), so caching its result can significantly improve the performance.\n\n### Example\n\n```php\nuse PiotrPress\\Cacher;\nuse PiotrPress\\WordPress\\Hooks;\nuse PiotrPress\\WordPress\\Hooks\\Action;\nuse PiotrPress\\WordPress\\Hooks\\Filter;\n\nclass Example {\n    #[ Action( 'init' ) ]\n    public function example_init() : void {\n        // do something\n    }\n\n    #[ Filter( 'the_title', 1 ) ]\n    public function example_the_title( string $post_title, int $post_id ) : string {\n        // do something\n    }\n}\n\n$example = new Example();\n$cache = new Cacher( '.hooks' );\n\nHooks::add( object: $example, cache: $cache );\nHooks::remove( object: $example, cache: $cache );\n```\n\n**Note:** You can use simple file-based cache, which is provided by [PiotrPress\\Cacher](https://github.com/PiotrPress/cacher) library distributed with this library.\n\n## Kudos\n\nInspirations, feedback, ideas and feature requests provided by:\n\n- [Jakub Mikita](https://github.com/jakubmikita)\n- [Sebastian Pisula](https://github.com/sebastianpisula)\n- [Mateusz Gbiorczyk](https://github.com/gbiorczyk)\n- [Krzysztof Grabania](https://github.com/Dartui)\n- [Dominik Kawula](https://github.com/domkawula)\n- [Jacek Sławiński](https://github.com/jacekslawinski)\n\n## Troubleshooting\n\n**Note:** Named arguments not working in PHP \u003c `8.0` version.\n\n## Requirements\n\nPHP \u003e= `7.4` version.\n\n## License\n\n[GPL3.0](license.txt)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrpress%2Fwordpress-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrpress%2Fwordpress-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrpress%2Fwordpress-hooks/lists"}