Skip to content

Repository files navigation

node-hooker

A powerful and flexible hook system for Node.js, inspired by the WordPress actions and filters API. This package provides a simple yet effective way to create extensible and decoupled architectures in your JavaScript applications.

VersionGitHub licenseGitHub starsGitHub forksGitHub watchersGitHub followersHire Me

[ It is designed to be cross-platform, supporting modern ES2015+ environments as well as vanilla JavaScript setups. ]

Features

  • Full WordPress Hook API compatibility: Implements the vast majority of WP hook functions.

  • Actions (do_action): Create points in your code where other functions can be executed.

  • Filters (apply_filters): Create points in your code where data can be modified.

  • Priority-based execution: Control the order in which callbacks are executed.

  • Hook Inspection: Check if hooks exist, how many times they've run, and what's currently running.

  • Easy to use: A simple and intuitive API.

  • Zero dependencies: Lightweight and self-contained.

Installation

You can install package via-

npm install

This is the standard way to install any Node.js package-

npm install node-hooker

Or-

Yarn install

Yes — Yarn can install npm packages because it uses the same npm registry under the hood (equivalent to npm install node-hooker)-

yarn add node-hooker

Test

After installation, you can run tests from the terminal using-

npm test

Quick Start

✅ NodeJS

consthooker=require('node-hooker');// --- ACTIONS ---// 1. Register a function for an action hookfunctionmyActionCallback(arg1,arg2){console.log(`Action running! Args: ${arg1}, ${arg2}`);console.log(`Currently doing action: ${hooker.current_action()}`);}hooker.add_action('app_init',myActionCallback,10,2);// 2. Trigger the action hookconsole.log('Doing action...');hooker.do_action('app_init','user_id_123',{setting: 'on'});console.log(`'app_init' has run ${hooker.did_action('app_init')} time(s).`);// --- FILTERS ---// 1. Register a function for a filter hookfunctionmyFilterCallback(text){returntext.toUpperCase();}hooker.add_filter('format_title',myFilterCallback);// 2. Trigger the filter hook and use the modified valuelettitle='Hello World';title=hooker.apply_filters('format_title',title);console.log(title);// Output: HELLO WORLD

OR

✅ Browser Usage (UMD)

This library can be used directly in the browser. A UMD (Universal Module Definition) bundle is provided in the dist folder, which is also available via CDN.

1. Include via CDN

You can add node-hooker to your project by including the following script tag. It's recommended to use the minified version for production.

<!-- Regular --><scriptsrc="https://cdn.jsdelivr.net/npm/node-hooker/dist/node-hooker.umd.min.js"></script><!-- Minified --><scriptsrc="https://cdn.jsdelivr.net/npm/node-hooker/dist/node-hooker.umd.js"></script>

2. Example Usage

Once included, the library will be available under the global variable Hooker.

<!DOCTYPE html><html><head><title>node-hooker Browser Example</title></head><body><h1>Check the console for output!</h1><scriptsrc="https://cdn.jsdelivr.net/npm/node-hooker/dist/node-hooker.umd.min.js"></script><script>// The library is now available on the window.Hooker objectconsole.log(Hooker);// Add an actionHooker.add_action('app_loaded',function(){console.log('The application has loaded!');});// Trigger the actionHooker.do_action('app_loaded');// Console Output: The application has loaded!// Use a filterconstoriginalText="hello browser";constfilteredText=Hooker.apply_filters('format_text',originalText,(text)=>text.toUpperCase());console.log(filteredText);// Note: Filters require a callback to be added first to have an effect.// Let's add one now.Hooker.add_filter('format_text',(text)=>text.toUpperCase());consttrulyFilteredText=Hooker.apply_filters('format_text',originalText);console.log(trulyFilteredText);// Console Output: HELLO BROWSER</script></body></html>

Preview

✅ NodeJS

node test.php

OR

✅ Browser (UMD)

▶ Open Live Preview


API Reference

1. Registering Hooks

FunctionPurpose
add_action(hook, callback, priority, args)Attach a function to an action hook.
add_filter(hook, callback, priority, args)Attach a function to a filter hook.

2. Triggering Hooks

FunctionPurpose
do_action(hook, ...args)Trigger an action hook (run all attached callbacks).
do_action_ref_array(hook, args_array)Same as do_action(), but pass arguments as an array.
apply_filters(hook, value, ...args)Trigger a filter hook, passing $value through each callback.
apply_filters_ref_array(hook, args_array)Same as apply_filters(), but arguments are passed as an array.

3. Removing Hooks

FunctionPurpose
remove_action(hook, callback, priority)Remove a previously added action callback.
remove_filter(hook, callback, priority)Remove a previously added filter callback.
remove_all_actions(hook, priority)Remove all callbacks for a specific action hook (or priority).
remove_all_filters(hook, priority)Remove all callbacks for a specific filter hook (or priority).

4. Inspecting Hooks

FunctionPurpose
has_action(hook, callback = false)Check if a function is hooked to an action (or if any callbacks exist).
has_filter(hook, callback = false)Check if a function is hooked to a filter (or if any callbacks exist).
did_action(hook)Returns how many times an action has run.
current_action()Get the name of the currently running action hook.
current_filter()Get the name of the currently running filter hook.
doing_action(hook = null)Check if a specific action (or any action) is currently running.
doing_filter(hook = null)Check if a specific filter (or any filter) is currently running.

Advanced Usage

✅ Custom Instances

While the singleton is convenient, you can create isolated instances of the Hooker class for separate event buses.

constmyInstance=newHooker();myInstance.add_action('my_hook',()=>console.log('Hello from my instance!'));myInstance.do_action('my_hook');

Files

Files structures/ tree

node-hooker/
├── lib/
│ └── Hooker.js
│ └── Hook.js
├── dist/
│ └── node-hooker.umd.js
│ └── node-hooker.umd.min.js
├── test.js
├── browser-test.html
├── package.json
├── README.md
└── CHANGELOG.md

License

This extensible codes is licensed under the MIT License. Copyright (c) 2025 by Mamedul Islam.

See the LICENSE file for more details.


Author & Hire Me

This extensible codes was created and is maintained by Mamedul Islam.

As a passionate web developer with experience in creating interactive and user-friendly web components. Currently available for freelance projects or full-time opportunities.

Helping businesses grow their online presence with custom web solutions. Specializing in WordPress, WooCommerce, and Shopify. Building modern, responsive, and high-performance scalable websites with custom made plugins, codes, customizations.

Hire Me


Show Your Support

If you find this extension useful, please consider giving it a star on GitHub! Your support helps motivate further development and improvements.

GitHub stars

If you found this project helpful, give it a ⭐ on GitHub!

About

A powerful and flexible hook system for Node.js, inspired by the WordPress actions and filters API.

Topics

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages