{"id":16044074,"url":"https://github.com/nylen/node-wp-hooks","last_synced_at":"2025-03-17T21:31:12.097Z","repository":{"id":57132583,"uuid":"181394880","full_name":"nylen/node-wp-hooks","owner":"nylen","description":"WordPress actions and filters in JS","archived":false,"fork":false,"pushed_at":"2019-04-15T20:06:12.000Z","size":151,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T04:46:49.016Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nylen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-15T01:59:12.000Z","updated_at":"2020-06-12T07:03:56.000Z","dependencies_parsed_at":"2022-09-02T18:10:22.507Z","dependency_job_id":null,"html_url":"https://github.com/nylen/node-wp-hooks","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Fnode-wp-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Fnode-wp-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Fnode-wp-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nylen%2Fnode-wp-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nylen","download_url":"https://codeload.github.com/nylen/node-wp-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243885933,"owners_count":20363644,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T00:03:32.465Z","updated_at":"2025-03-17T21:31:11.858Z","avatar_url":"https://github.com/nylen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @nylen/wp-hooks [![Build Status](https://img.shields.io/travis/nylen/node-wp-hooks/master.svg)](https://travis-ci.org/nylen/node-wp-hooks) [![Coverage](https://img.shields.io/coveralls/nylen/node-wp-hooks/master.svg)](https://coveralls.io/github/nylen/node-wp-hooks)\n\n**WordPress-style hooks** (_actions_ and _filters_) for Node.js.\n\nRecent (5.x) versions of WordPress also include a JavaScript actions and\nfilters library.  **It is not exactly the same as this library** - this\nlibrary shares history with the WordPress version, but it is simpler and is\nmainly intended for usage in Node.js server programs.\n\nWhen combined with a mechanism for plugin registration and loading (outside the\nscope of this library), this is a simple, effective pattern that allows\nmodifying values or executing other code at key parts in your app.\n\nMore information about actions and filters as they are used in WordPress:\nhttps://developer.wordpress.org/plugins/hooks/\n\n## Usage\n\n```js\nconst WPHooks = require( '@nylen/wp-hooks' );\nconst hooks = new WPHooks();\n```\n\n* **hooks.addAction( 'identifier', callback, priority )** - Add a function to the\n  action given by `identifier`. Actions do not have return values.\n* **hooks.addFilter( 'identifier', callback, priority )** - Add a function to the\n  filter given by `identifier`. Filters have return values, and functions\n  hooked into a filter can modify its return value.\n* **hooks.removeAction( 'identifier', callback )** - Remove an action.\n* **hooks.removeFilter( 'identifier',  callback )** - Remove a filter.\n* **hooks.removeAllActions(  'identifier' )** - Remove all actions from the given\n  `identifier`.\n* **hooks.removeAllFilters(  'identifier' )** - Remove all filters from the given\n  `identifier`.\n* **hooks.doAction( 'identifier', arg1, ... )** - Run an action along with any\n  functions hooked into it.\n* **hooks.applyFilters( 'identifier', arg1, ... )** - Run a filter along with any\n  functions hooked into it, and return the default value or the value from the\n  last function.\n* **hooks.doingAction( 'identifier' )** - Whether this object is currently\n  executing the action with the name `identifier`.\n* **hooks.doingFilter( 'identifier' )** - Whether this object is currently\n  executing the filter with the name `identifier`.\n* **hooks.didAction( 'identifier' )** - Whether this object has executed the\n  action with the name `identifier`.\n* **hooks.didFilter( 'identifier' )** - Whether this object has executed the\n  filter with the name `identifier`.\n* **hooks.hasAction( 'identifier' )** - Whether this object has any functions\n  hooked into the action with the name `identifier`.\n* **hooks.hasFilter( 'identifier' )** - Whether this object has any functions\n  hooked into the filter with the name `identifier`.\n\nIn large apps, it is a good idea to enforce separation of different types of\nhooks by either prefixing `identifier` hook names with `namespace.identifier`,\nor (even better) using separate `hooks` objects for each part of the app.\n\n## Sync or Async?\n\nAll filters and actions are **synchronous** by default, and this library\ncontains no special code for async callbacks or promises.\n\nHowever, because `async` functions just return `Promise` objects under the\ncovers, you can easily pass a `Promise` through a chain of hooks.  If you\nfollow a couple of simple rules, then the final result from `applyFilters` will\nbe a promise that resolves or fails when all of its attached functions have\nfinished processing.\n\nFor hooks that may require asynchronous behavior, the app must use a **filter**\nrather than an **action**.  Then the app can start the filter chain normally,\nand just `await` the final result, which will be a `Promise` returned from the\nlast function in the filter chain:\n\n```js\nconst finalValue = await applyFilters(\n  'my_async_code_path',\n  'defaultValue'\n);\n```\n\nThen, individual filters can be written as `async` functions, as long as they\nadhere to a simple convention:  **Accept a `Promise` as an argument and `await`\nit before returning.**\n\n```js\naddFilter( 'my_async_code_path', async p =\u003e {\n  const value = await p;\n  return value + '/modified';\n} );\n```\n\nThis allows for any function in the chain of filters to return a `Promise`,\nwhich other functions will wait for before doing their processing.  (It's fine\nto `await` a non-`Promise` value too.)\n\nSee `index.test.js` for more examples and information.\n\nFuture **major versions** of this library may change the behavior around\nasynchronous computations.  Currently this mostly works (with the exception of\n`doingFilter` and `didFilter`), but requires adhering to a non-obvious\nconvention and can be difficult to understand and use effectively.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnylen%2Fnode-wp-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnylen%2Fnode-wp-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnylen%2Fnode-wp-hooks/lists"}