Download the PHP package akirk/extract-wp-hooks without Composer
On this page you can find all versions of the php package akirk/extract-wp-hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download akirk/extract-wp-hooks
More information about akirk/extract-wp-hooks
Files in akirk/extract-wp-hooks
Package extract-wp-hooks
Short Description Extract available WordPress hooks for a Github Wiki
License GPL-2.0-or-later
Informations about the package extract-wp-hooks
extract-hooks
This script is intended for WordPress plugins that provide hooks that can be used by other plugins. By parsing its source code, it creates a documentation in a Github wiki.
You can configure this tool either through a JSON configuration file or directly through GitHub Action inputs. For local usage, you'll need a configuration file, but for GitHub Actions, you can specify all configuration directly in your workflow file.
Examples
- https://github.com/akirk/extract-hooks/wiki/Hooks (extracted from example.php)
- https://github.com/akirk/friends/wiki/Hooks
- https://github.com/akirk/enable-mastodon-apps/wiki/Hooks
How it works
The PHP script doesn't have any dependencies. It uses PHP's internal parser (using token_get_all
) to identify PHP function calls to apply_filters()
or do_action()
.
It generates a markdown file for each filter which is suitable for a Github wiki. The page contains potentially provided documentation (via a comment in the source code), an (auto-generated) example, parameters, return value, references to the source code (including extracted source snippet).
Example: Provide Documentation Via a Comment
For each filter, it looks at the comment preceeding the filter, so that you can document it, for example:
This will generate an example_filter1.md that contains the text This is an example filter
and a list of parameters and return value:
example_filter1
This is an example filter.
Parameters
string
$text
The text to modify.string
$mode
Extra information that might be useful.Returns
string
The modified text.
But not only that, it will contain an auto-generated example:
Auto-generated Example
Provide an Example
You can also provide your own example in the comment, that will override the auto-generated example:
php
- add_filter( 'example_filter2', function ( $text ) {
- return strtolower( $text );
- } );
It generates this output: example_filter2
Example
No Documentation
Finally, if you have an filter without any documentation, the script attempts to create a useful auto-generated example. So suppose you have code
It generates this output: example_filter3
Auto-generated Example
Parameters
$text
$mode
Install
Option 1: GitHub Action (Recommended)
Add the following workflow file to your WordPress plugin repository at .github/workflows/extract-hooks.yml
:
This will automatically extract hooks from your PHP files and update your GitHub wiki whenever you push changes.
Configuration
You can configure the action in two ways:
-
Using action inputs (recommended):
- Using a configuration file:
Create an .extract-wp-hooks.json
file in your repository root:
Available Action Inputs
Input | Description | Default |
---|---|---|
namespace |
PHP Namespace that's used | |
base-dir |
Base directory to scan for hooks | . |
wiki-directory |
Directory to store wiki files | wiki |
github-blob-url |
GitHub blob URL for source links | Auto-generated from repository |
exclude-dirs |
Comma-separated list of directories to exclude | vendor,node_modules |
ignore-filters |
Comma-separated list of filter names to ignore | |
ignore-regex |
Regex pattern to ignore filter names | |
section |
How to group hooks in documentation: 'file' or 'dir' | file |
wiki-repo |
Wiki repository URL (e.g., username/repo.wiki.git) | Auto-generated from repository |
config-file |
Path to config file (optional if other inputs are provided) | .extract-wp-hooks.json |
Option 2: Composer
Via composer:
You will then be able to run extract-wp-hooks.php
from the vendor bin directory:
Place a .extract-wp-hooks.json
or extract-wp-hooks.json
in your project directory to use it.