Giter Site home page Giter Site logo

WordPress Hooks Reference

All the actions and filters from WordPress core in machine-readable JSON format.

Last updated for WordPress 6.4.

Installation

  • As a Composer package for use in PHP:
    composer require wp-hooks/wordpress-core
  • As an npm package for use in JavaScript or TypeScript:
    npm install @wp-hooks/wordpress-core

Usage in PHP

// Get hooks as JSON:
$actions_json = file_get_contents( 'vendor/wp-hooks/wordpress-core/hooks/actions.json' );
$filters_json = file_get_contents( 'vendor/wp-hooks/wordpress-core/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 ( false !== strpos( $hook['name'], $search ) );
} );

var_dump( $results );

Usage in JavaScript

// Get hooks as array of objects:
const actions = require('@wp-hooks/wordpress-core/hooks/actions.json').hooks;
const filters = require('@wp-hooks/wordpress-core/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);

Importing in TypeScript

import { hooks as actions } from '@wp-hooks/wordpress-core/hooks/actions.json';
import { hooks as filters } from '@wp-hooks/wordpress-core/hooks/filters.json';

Interfaces for the components of the hooks can be imported too, if you need them:

import { Hooks, Hook, Doc, Tags, Tag } from '@wp-hooks/wordpress-core/interface';

Actions, Filters, and Schemas

What can I use this for?

Anything that needs programmatic access to a list of available hooks, for example:

Regenerating the Hook Files

Change the roots/wordpress-full version in composer.json to the required WordPress version, or dev-main for nightly.

Install the dependencies:

npm i && composer i

Then run:

composer generate

Some scripts are available for checking the data:

  • Check everything:
    npm run check
  • Find hooks with missing @since tags:
    jq '.hooks[] | . as $d | .doc .tags | map(.name) | select( contains(["since"]) | not ) | $d' hooks/filters.json
    jq '.hooks[] | . as $d | .doc .tags | map(.name) | select( contains(["since"]) | not ) | $d' hooks/actions.json
  • Find hooks with incorrect number of @param tags (not completely accurate, not sure why):
    jq '.hooks[] | select( .args == ( .doc.tags | map(.name) | select( contains(["param"]) ) | length ) ) | .name' hooks/filters.json
    jq '.hooks[] | select( .args == ( .doc.tags | map(.name) | select( contains(["param"]) ) | length ) ) | .name' hooks/actions.json

Hook Files for Plugins

Do you want the same hook files for your favourite plugins? The hook files in this repo are generated using wp-hooks-generator. It can generate hook files for WordPress plugins and themes as well as core.

Sponsors

The time that I spend maintaining this library and others is in part sponsored by:

Automattic

Plus all my kind sponsors on GitHub:

Sponsors

Click here to find out about supporting this library and my other WordPress development tools and plugins.

WP Hooks's Projects

generator icon generator

Generates a JSON representation of the WordPress actions and filters in your code

parser icon parser

File scanning and hook parsing functionality for WordPress core, plugins, and themes

php-api icon php-api

A PHP API for interacting with wp-hooks definitions

vscode-wordpress-hooks icon vscode-wordpress-hooks

VS Code extension that provides intellisense and autocomplete for WordPress hooks, actions, and filters

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.