offsetwp/hook

Typed, secure, modern, and object-oriented hook registration for WordPress

Maintainers

Details

github.com/offsetwp/hook

Source

Issues

Fund package maintenance!
Buy Me A Coffee
Ko Fi

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/offsetwp/hook

1.1.0 2025-12-18 17:05 UTC

This package is not auto-updated.

Last update: 2025-12-19 14:17:50 UTC


README

OffsetWP Hook OffsetWP Hook

OffsetWP Hook

Typed, secure, modern, and object-oriented hook registration for WordPress

Installation

composer require offsetwp/hook

Basic usage

Add a basic action

use OffsetWP\Hook\Support\Action;

new class extends Action {
	public string $hook_name = 'init';

	protected function handle() {
		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\Filter;

new class extends Filter {
	public string $hook_name = 'admin_footer_text';

	protected function handle( $text ) {
		return 'Made with love by OffsetWP | ' . $text;
	}
};

Add a basic shortcode

use OffsetWP\Hook\Support\ShortCode;

new class extends ShortCode {
	public string $hook_name = 'my_shortcode';

	protected function handle( 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\Filter;

new class extends Filter {
	public string $hook_name       = 'admin_footer_text';
	public string $hook_callback   = 'launch'; // default: `handle`
	public int $hook_priority      = 10; // default: 10
	public int $hook_accepted_args = 1; // default: 1

	protected function launch( $text ) {
		return 'Made with love by OffsetWP | ' . $text;
	}
};

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\Support\Filter;

new class extends Filter {
	public string $hook_name = 'admin_footer_text';

	public function __construct( private $myServiceName = 'CustomService' ) {
		return parent::__construct();
	}

	protected function handle( $text ) {
		return $this->myServiceName . ' | ' . $text;
	}
};

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