onepix / wordpress-hooks-stub-generator
Generates a JSON representation of the WordPress actions and filters in your code
Fund package maintenance!
johnbillion
Requires
- php: >=8.1
- ext-dom: *
- ext-libxml: *
- erusev/parsedown: 1.7.4
- nikic/php-parser: ^v4.17.1
- phpdocumentor/reflection-docblock: ^5.6.1
- symfony/console: ^6.4
- symfony/filesystem: ^6.4
- symfony/finder: ^6.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.65
- mikey179/vfsstream: ^v1.6.12
- opis/json-schema: 2.3.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
Replaces
README
Generates a JSON representation of the WordPress actions and filters in your code. Can be used with WordPress plugins, themes, and core.
Installation
composer require onepix/wordpress-hooks-stub-generator
Requirements
PHP: 8.1+
Generating the Hook Files
./bin/generate-hooks --input=src --input=additional --output=hooks
Usage of the Generated Hook Files in PHP
// Get hooks as JSON: $actions_json = file_get_contents( 'hooks/actions.json' ); $filters_json = file_get_contents( 'hooks/filters.json' ); // Convert hooks to PHP: $actions = json_decode( $actions_json, true )['hooks']; $filters = json_decode( $filters_json, true )['hooks']; // Search for filters matching a string: $search = 'permalink'; $results = array_filter( $filters, function( array $hook ) use ( $search ) { return ( strpos( $hook['name'], $search ) !== false ); } ); var_dump( $results );
Usage of the Generated Hook Files in JavaScript
// Get hooks as array of objects: const actions = require('hooks/actions.json').hooks; const filters = require('hooks/filters.json').hooks; // Search for actions matching a string: const search = 'menu'; const results = actions.filter( hook => ( hook.name.match( search ) !== null ) ); console.log(results);
Ignoring Files or Directories
You can ignore files or directories in two ways:
On the Command Line
./vendor/bin/wp-hooks-generator --input=src --output=hooks --ignore-files="ignore/this,ignore/that"
Ignoring Hooks
You can ignore hooks in two ways:
On the Command Line
./vendor/bin/wp-hooks-generator --input=src --output=hooks --ignore-hooks="this_hook,that_hook"
TypeScript Interfaces for the Hook Files
The TypeScript interfaces for the hook files can be found in interface/index.d.ts
. Usage:
import { Hooks, Hook, Doc, Tags, Tag } from 'hooks/index.d.ts';
JSON Schema for the Hook Files
The JSON schema for the hook files can be found in schema.json
.