Data hooks run code before or after certain interactions with your site's collections. A data hook allows you to intercept the interaction immediately before or immediately after it occurs. The hook's code can even be used to affect the interaction itself. For example, you may want to intercept an item before it is added to your collection to perform a final validation or tweak the data that actually makes it into the collection.
In general, hooks are run whether the interaction with your collection is
initiated by a page element, programmatically, or when using the Content Management System (CMS).
However, a Data API call from the backend code of your site may pass the
WixDataOptions object and use it
to suppress hooks from being called on that particular operation.
Code for the hooks is written in the data.js file, which resides in the Backend section of your site. Hook functions are defined using the following pattern:
You can add these functions to the data.js file by writing them yourself, or by generating templates using the Velo sidebar.
To add a hook to a collection from the sidebar:
- Click the Databases button and hover over the relevant collection.
- Click the Show More icon.
- Select Add/Remove Hooks, and choose the hooks you want to add.
Was this helpful?