> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: Create Dynamic Page Hooks ## Article: Create Dynamic Page Hooks ## Article Link: https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/datasets-and-dynamic-pages/create-dynamic-page-hooks.md ## Article Content: # Create Dynamic Page Hooks You create dynamic page hooks to add custom logic during the page routing and loading process. Use hooks to validate requests, modify data queries, customize responses, or update sitemaps for dynamic pages. ## Before you begin Make sure you have: - A dynamic page set up. - A backend `routers.js` file. ## Add a `beforeRouter` hook To add a `beforeRouter()` hook on dynamic pages with the **listings** prefix, add the following code to your `routers.js` file. A `beforeRouter` hook needs to return a router response object. This hook restricts admin pages to visitors with the admin role: ```javascript import { forbidden, next } from "wix-router"; export function listings_beforeRouter(request) { // Restrict admin pages to site owners only if (request.path.length > 1 && request.path[0] === "admin") { if (request.user?.role === "Admin") { return next(); } return forbidden(); } return next(); } ``` ## Add a `customizeQuery` hook To add a `customizeQuery()` hook on dynamic pages with the **listings** prefix, add the following code to your `routers.js` file. A `customizeQuery` hook needs to return a data query object. This hook shows only active real estate listings to non-admin visitors: ```javascript export function listings_customizeQuery(request, route, query) { // Show only active listings to regular visitors if (request.user?.role !== "Admin") { return query.eq("status", "active"); } return query; } ``` ## Add an `afterRouter` hook To add an `afterRouter()` hook on dynamic pages with the **listings** prefix, add the following code to your `routers.js` file. An `afterRouter` hook needs to return a router response object. This hook redirects unavailable listings and uses custom templates for featured listings: ```javascript import { redirect, ok } from "wix-router"; export function listings_afterRouter(request, response) { // Redirect if listing is unavailable if (response.status === 200 && response.data?.status === "unavailable") { return redirect("/unavailable"); } // Use different template for featured listings if (response.data?.featured) { return ok("featured-listing-page", response.data, response.head); } return response; } ``` ## See also - [About dynamic page hooks](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/datasets-and-dynamic-pages/about-dynamic-page-hooks.md) - [About dynamic pages and code](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/datasets-and-dynamic-pages/about-dynamic-pages-and-code.md) - [Router API reference](https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/introduction.md)