offsetwp / hook
Fund package maintenance!
Buy Me A Coffee
Ko Fi
Requires
- php: >=8.1
Requires (Dev)
This package is not auto-updated.
Last update: 2025-08-05 21:32:50 UTC
README
OffsetWP Hook
A library to manage WordPress actions, filters and shortcodes hooks
Installation
composer require offsetwp/hook
Basic usage
Add a basic action
use Offsetwp\Hook\Support\AddAction; new class extends AddAction { public string $hook_name = 'init'; public function execute() { register_post_type( 'project', array( 'labels' => array( 'name' => __( 'Projects', 'offsetwp' ), 'singular_name' => __( 'Project', 'offsetwp' ), ), 'public' => true, 'has_archive' => true, ) ); } };
Add a basic filter
use Offsetwp\Hook\Support\AddFilter; new class extends AddFilter { public string $hook_name = 'admin_footer_text'; public function execute( $text ) { return 'Made with love by OffsetWP | ' . $text; } };
Add a basic shortcode
use Offsetwp\Hook\Support\AddShortCode; new class extends AddShortCode { public string $hook_name = 'my_shortcode'; public function execute( array $atts, string $content, string $shortcode_tag ) { return 'My shortcode'; } };
Advanced hook
Hooks all work in the same way: they have a name, a callback function, the order of priority in which they are to be executed, and the number of callback parameters. Only shortcodes have only the hook name and a callback function.
For greater flexibility, it is possible to override some properties without affecting others. Here is an filter with all possible properties:
use Offsetwp\Hook\Support\AddFilter; new class extends AddFilter { public string $hook_name = 'admin_footer_text'; public string $hook_callback = 'launch'; // default: `execute` public int $hook_priority = 10; // default: 10 public int $hook_accepted_args = 1; // default: 1 public function launch( $text ) { return 'Made with love by OffsetWP | ' . $text; } };
Use the WordPress core hooks
WordPress makes extensive use of hooks for its internal functioning. OffsetWP Hook provides ready-to-use wrappers for the most common native hooks.
They are available in:
Offsetwp\Hook\WordPress\Action
Offsetwp\Hook\WordPress\Filter
The wrappers already include the exact callback signatures, enabling optimal autocompletion in your IDE.
Basic usage
use Offsetwp\Hook\WordPress\Filter\WPAdminFooterTextAddFilter; new class extends WPAdminFooterTextAddFilter { public function execute( $text ) { return 'Made with love by OffsetWP | ' . $text; } };
Hook with a dynamique name
use Offsetwp\Hook\WordPress\Filter\WPGetMetaTypeMetadataAddFilter; new class extends WPGetMetaTypeMetadataAddFilter { public string $hook_name = 'get_post_metadata'; // 'get_{$meta_type}_metadata' public function execute( $value, $object_id, $meta_key, $single, $meta_type ) { return $value; } };
FAQ
Why are there only functions for add_action
, add_filter
, and add_shortcode
?
The goal of this project is to modernize and simplify development on WordPress. We started with hooks, as they are the most commonly used.
As soon as we identify an elegant way to integrate the following features, they will be added: do_action
apply_filters
remove_action
remove_filter