Typed, secure, and maintainable WordPress Core action and filter hooks
WordPress makes extensive use of hooks for its internal functioning. Unfortunately, the way it was developed makes it difficult to work with hook data:
- the callback is public and accessible by any other function
- to find out the returned parameters, you have to consult the documentation
- no auto-completion
OffsetWP Hook provides ready-to-use wrappers for native WordPress hooks. The wrappers already include the exact callback signatures, allowing for optimal autocompletion in your IDE.
composer require offsetwp/hookuse OffsetWP\Hook\WordPress\Filter\WPAdminFooterTextFilter;
new class extends WPAdminFooterTextFilter {
protected function handle( $text ) {
return 'Made with love by OffsetWP | ' . $text;
}
};use OffsetWP\Hook\WordPress\Filter\WPGetMetaTypeMetadataFilter;
new class extends WPGetMetaTypeMetadataFilter {
public string $hook_name = 'get_post_metadata'; // 'get_{$meta_type}_metadata'
protected function handle( $value, $object_id, $meta_key, $single, $meta_type ) {
return $value;
}
};With more substantial development, it may be necessary for your hooks to use dependencies. All of this is possible, depending on your needs:
use OffsetWP\Hook\WordPress\Filter\WPAdminFooterTextFilter;
new class extends WPAdminFooterTextFilter {
public function __construct( private $myServiceName = 'CustomService' ) {
return parent::__construct();
}
protected function handle( $text ) {
return $this->myServiceName . ' | ' . $text;
}
}
