Controllers
In HivePress, controllers are PHP classes that are used to define URL routes and implement actions corresponding to them. For example, the Listing
controller contains all the routes and actions related to listings (e.g. viewing, creating, updating and deleting).
Creating controllers
If you are developing a HivePress extension that requires custom URL routes, you should create a new controller. To do this, create a new class-{extension-name}.php
file (use lowercase letters, numbers, and hyphens only) in the includes/controllers
extension subdirectory and HivePress will load it automatically.
Then, define the controller PHP class. The class name should be based on the file name but with underscores instead of hyphens and no lowercase restriction (e.g. Foo_Bar
class for the class-foo-bar.php
file). Pick a name that is unique enough to avoid conflicts with other HivePress controllers.
Adding routes
Each URL route is defined as an array of parameters in the controller's class constructor and can have the redirect and action functions implemented as the class methods.
If you add new or change any of the existing URL routes, don't forget to refresh permalinks in the Settings > Permalinks section.
Once you add a custom route, you can get its URL anywhere by the route name:
Template routes
These routes are used for redirecting and rendering the page templates. For example, when you visit the listing edit page, the listing_edit_page
route checks if you have permission to edit this listing (or redirects you otherwise) and then renders the page template.
To define a new URL route, add an array of the following parameters:
title - text used as the page title and menu label;
path - the relative route URL path (starts with a slash);
base - the route name to inherit the
path
from;redirect - points to the route redirect function;
action - points to the route action function.
The code below defines a route with the /custom-route
URL that checks if the current user is logged in and, if so, redirects to the account page. If not, it renders the login page template.
The redirect and action functions can be implemented below the class constructor:
In the same way, you can add URL routes for any pages required for your extension.
REST API routes
These routes are used for receiving HTTP requests, performing specific actions and returning a JSON response. For example, when you edit a listing, the form sends a request to the listing_update_action
route, updating the listing based on the field values.
To define a new REST API route, add an array with the following parameters:
path - the relative route URL path (starts with a slash);
base - the route name to inherit the
path
from;method - the accepted HTTP method (e.g.
GET
,POST
);action - points to the route action function;
rest - flag required for REST API routes.
The code below defines a REST API route with the /wp-json/hivepress/v1/custom-route
URL, accepts requests via the POST
method and calls the custom_action
function.
The action function can be implemented below the class constructor:
Similarly, you can add REST API routes for any actions required for your extension.
Customizing routes
You can customize any of the available URL routes via the hivepress/v1/routes
filter hook. For example, the code below changes the Add Listing page URL:
Last updated