Generates a list of WordPress actions and filters from code and outputs them as JSON

Overview

wp-hooks-generator

Generates a JSON representation of the WordPress actions and filters in your code. Can be used with WordPress plugins, themes, and core.

Note: If you just want the hook files without generating them yourself, use the following packages instead:

Installation

composer require johnbillion/wp-hooks-generator

Generating the Hook Files

./bin/wp-hooks-generator --input=src --output=hooks

Usage of the Generated Hook Files in PHP

// Get hooks as JSON:
$actions_json = file_get_contents( 'hook/actions.json' );
$filters_json = file_get_contents( 'hook/filters.json' );

// Get hooks as 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 ( false !== strpos( $hook['name'], $search ) );
} );

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 => ( null !== hook.name.match( search ) ) );

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"

In composer.json

"extra": {
    "wp-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"

In composer.json

"extra": {
    "wp-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.

Comments
  • Issues with array notation

    Issues with array notation

    e.g.

    * @param array<string, array{name: string, class: string, placeholder?: string, type: string, label: string, required: bool}> $form_fields array of form fields and attributes
    * @param array<string, string>|false                                                                                          $saved_form saved form values
    

    isn't parsed correctly and ends up completely garbled in the .json

    opened by kkmuffme 6
  • Error on executing the binary

    Error on executing the binary

    @johnbillion

    I'm including this library in my plugin via composer and then tried executing "php ./vendor/bin/wp-hooks-generator " from plugin's root folder in the command line (Ubuntu server) but got the following error:

    `dir=$(cd "${0%[/\]*}" > /dev/null; cd ../johnbillion/wp-hooks-generator/bin && pwd)

    if [ -d /proc/cygdrive ]; then case $(which php) in $(readlink -n /proc/cygdrive)/*) # We are in Cygwin using Windows php, so the path must be translated dir=$(cygpath -m "$dir"); ;; esac fi

    "${dir}/wp-hooks-generator" "$@"`

    Any idea what went wrong? ...or further examples on how to use this library properly?

    My intention is to use the generator to list all hooks from all the WordPress plugins installed. So, gussing I should put the plugins root folder in the --src= parameter, but can not proceed to that step just yet.

    Thanks!

    opened by qriouslad 6
  • Update dependences versions

    Update dependences versions

    As now install this create conflicts with Psalm as example because of php-parser package.

    Maybe they are old (2 years~) in the composer.json and just need a bump.

    opened by Mte90 5
  • Output file that contains a parse error

    Output file that contains a parse error

    Parse Error: Syntax error, unexpected '?', expecting T_VARIABLE on line 14Parse Error: Syntax error, unexpected '?', expecting T_VARIABLE on line 138Parse Error: Syntax error, unexpected '?', expecting T_VARIABLE on line 14Done

    No idea in which files the tool encountered a parse error. Would it be possible to include the file name in the error message?

    opened by kkmuffme 4
  • add deprecated field

    add deprecated field

    would be nice if filters/action that are called with do_action_deprecated or apply_filters_deprecated had a field deprecated: true or something

    Or action_deprecated/filter_deprecated type would maybe even make more sense

    Happy to provide a PR

    opened by kkmuffme 2
  • Multiline filter parse error

    Multiline filter parse error

    Parse Error: Syntax error, unexpected ')' on line ...

    when doing this:

    /**
     * Get packages details
     *
     * @param array
     * @param int $order_id
     */
    $hello = apply_filters(
    	'hello_world',
    	array(),
    	$order_id,
    );
    
    opened by kkmuffme 2
  • Bump minimist from 1.2.0 to 1.2.5

    Bump minimist from 1.2.0 to 1.2.5

    Bumps minimist from 1.2.0 to 1.2.5.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 1
  • Support for aliases

    Support for aliases

    Fixes https://github.com/johnbillion/wp-hooks/issues/6.

    Unfortunately a specific PHPDoc tag never got implemented to document the common values for dynamic hook names, but we have now got a mostly standardised use of "Possible hook names include:" followed by an unordered list (example: https://developer.wordpress.org/reference/hooks/type_template_hierarchy/), so we can parse that list if it exists and store the result in a new property.

    I've hooked up the resulting aliases to the autocomplete and hover provision in WordPress Hooks Intellisense for VS Code:

    opened by johnbillion 0
  • skip file if it's non readable

    skip file if it's non readable

    Reflector gives an error "The given file should be a string, should exist on the filesystem and should be readable" if the file is not readable.

    This seems to be an issue with what is going on in the background with the old version of reflector used here, as I did not encounter this issue with other libraries or the latest version of reflector when I run (e.g. psalm) on same files. This just snoozes the error.

    opened by kkmuffme 0
  • Parse Error with Immediately Invoked Function Expression (IIFE)

    Parse Error with Immediately Invoked Function Expression (IIFE)

    I have the following function that fails parsing because of the () call at the end:

    $container = $container ?: ( function() {
    	// logic here
    } )();
    

    IIFE are supported as of PHP 7.0 and up.

    opened by senadir 4
  • include hooks/actions called by scheduled functions

    include hooks/actions called by scheduled functions

    Closes https://github.com/johnbillion/wp-hooks-generator/issues/9

    The "docs" are always empty, since a) nobody ever documents these and b) if someone really wants to document, we can still add that later.

    Used the original nodes for all things (instead of the WP Reflector nodes), as this code is/will be used in other repos that only use the phpDocumentor/Reflector directly (and I need args from the reflector node anyway, so it's easier to maintain, as it's the widely used reflector syntax instead of the WP mumbo jumbo)

    opened by kkmuffme 1
  • Hooks of WP cron schedule events

    Hooks of WP cron schedule events

    https://developer.wordpress.org/reference/functions/wp_schedule_single_event/ https://developer.wordpress.org/reference/functions/wp_schedule_event/

    Both can declare hooks that will be called. These should be included in actions.json ?

    opened by kkmuffme 6
  • Provide long descriptions in their raw markdown

    Provide long descriptions in their raw markdown

    The WP Parser library converts long descriptions for hooks into HTML from their source markdown. A consumer may want the raw markdown so it can convert it itself instead of having to deal with HTML.

    opened by johnbillion 0
Owner
John Blackbourn
John Blackbourn
Disable direct access to your sites /wp-login.php script, plus user notifications based on actions.

WordPress Login Locker Disable direct access to your sites /wp-login.php script plus user notifications based on actions. Package Installation (via Co

Austin Passy 3 Nov 23, 2021
List of high-ranking websites powered by WordPress (everything but news and blogs)

Powered By WordPress About 25% of the web is powered by WordPress. A big majority of these sites are private blogs but also heavy-weights such as Sony

MB 19 Dec 23, 2022
Curated list that contain code, snippets or examples without libraries or external packages for developers.

Awesome WordPress Developer Tips Curated list that contain very awesome and ready code, snippets or examples without libraries or external packages ma

Daniele Scasciafratte 110 Nov 13, 2022
A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings.

Suspend WP A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings. ?? Demo (coming so

Waren Gonzaga 3 Nov 15, 2021
Create WordPress themes with beautiful OOP code and the Twig Template Engine

By Jared Novack (@jarednova), Lukas Gächter (@lgaechter), Pascal Knecht (@pascalknecht), Maciej Palmowski (@palmiak_fp), Coby Tamayo (@cobytamayo), Up

Timber 5.2k Jan 5, 2023
Awesome Enterprise - a shortcode based low code platform for PHP and WordPress.

Awesome Enterprise Framework Awesome Enterprise is a shortcode based low code platform for PHP and WordPress. You can set it up using composer compose

WPoets - Your WordPress Experts 10 Nov 17, 2022
Simple WordPress plugin to learn how to understand WordPress Crons and the Action Scheduler library.

Simple WordPress plugin to learn how to understand WordPress Crons and the Action Scheduler library. Import Jamendo playlists with tracks in WordPress posts.

Pierre Saikali 3 Dec 7, 2022
This is code to create a new user as admin use for Wordpress FrontEnd Developer to prevent any scaming from clients

theme-setup This is code to create a new user as admin use for Wordpress FrontEnd Developer to prevent any scaming from clients How to use Just copy c

Abdul Razzaq 1 Nov 27, 2021
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.

WP Bootstrap Navwalker This code in the main repo branch is undergoing a big shakeup to bring it in line with recent standards and to merge and test t

WP Bootstrap 3.3k Jan 5, 2023
The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone.

Pronamic WordPress Basecone The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone. Table of contents Au

Pronamic 1 Oct 19, 2021
Twenty Twenty-Two, the default WordPress theme that will launch with WordPress 5.9.

Twenty Twenty-Two Welcome to the development repository for the default theme that will launch with WordPress 5.9. About Twenty Twenty-Two is designed

null 414 Nov 28, 2022
Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer.

Description Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer. Support [audio] tag, compatible with AMP. Requirement WordPr

Karl Chen 24 Nov 3, 2022
WordPress plugin that lets you use Discourse as the community engine for a WordPress blog

WP Discourse Note: the wp-discourse plugin requires >= PHP-5.4.0. The WP Discourse plugin acts as an interface between your WordPress site and your Di

Discourse 497 Dec 10, 2022
WordPress & TypeScript. Simple starter template for WordPress projects

WordPress & TypeScript. Simple starter template for WordPress projects that want to use TypeScript in combination with @wordpress/scripts

Make it WorkPress 11 Sep 27, 2022
A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API

Wordpress XML-RPC PHP Client A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API Created by Hieu Le MIT licensed. Cur

Hieu Le 112 Nov 10, 2022
Authentication for WPGraphQL using JWT (JSON Web Tokens)

WPGraphQL JWT Authentication This plugin extends the WPGraphQL plugin to provide authentication using JWT (JSON Web Tokens) JSON Web Tokens are an ope

WPGraphQL 268 Dec 31, 2022
🚀WordPress Plugin Boilerplate using modern web techs like TypeScript, SASS, and so on... on top of a local development environment with Docker and predefined GitLab CI for continous integration and deployment!

WP React Starter: WordPress React Boilerplate DEPRECATED: WP React Starter was a "research project" of devowl.io for the development of our WordPress

devowl.io GmbH 344 Jan 1, 2023
Scaffold plugin for creating and managing Blocks, Block Patterns, Block Styles and Block Editor Sidebars in the WordPress Block Editor (aka Gutenberg).

WordPress Block Editor Scaffold This project is a template repo for developing WordPress Blocks, Block Patterns, Block Styles and Block Editor Sidebar

Rareview 6 Aug 2, 2022
Beauty and simple Wordpress video player plugin. Powerfull and lite in use.

Sonic Spectre Video Player Beauty and simple Wordpress video player plugin. Powerfull and lite in use. Quick Start: Download plugin from this repo and

Halcyon 1 Jan 4, 2022