Router
This component implements callbacks and methods for managing URLs and redirects. In HivePress, each URL is registered as a route with an array of parameters. This makes URLs customizable and accessible via the Router methods.
To get the current route name, call the get_current_route_name
method:
You can also customize any URL route via the hivepress/v1/routes
filter hook. Remember to refresh permalinks in Settings > Permalinks after making any URL changes.
Quick example
The code example below redirects non-registered users from the listing page to the login page. After the user is logged in or registered, there's a redirect back to the initial listing page.
Getting URLs
To get the current URL, call the get_current_url
method:
For getting a URL by the route name, use the get_url
method:
Some routes require extra URL parameters. The code example below gets a listing URL by ID:
To get a URL that redirects users back after some action (e.g. logging in), use the get_return_url
method:
If you need to get the redirect URL from the current URL query parameters, call the get_redirect_url
method:
Last updated