WPJobBoard router is very powerful “thing”. Basically it allows to modify all permalinks from single ini file. However (like uncle Ben said) with great power comes great responsibility, so before you will start playing with the router read this article carefully.
How Router works?
When WPJobBoard will confirm (in the “wp” filter) that currently displayed page will be a job board or resumes module then the router is executed to detect which job board page (index, add job form, apply form etc.) will be displayed.
The detection is based on URL path. For example by default empty path is a main job board page, “/add” path is a “Add a job” form, “view/(job-name)” is job details page. Each job has unique (job-name) so usually detection is based on regular expressions.
Route files
There are two routes files (actually 3 but the third one is for admin routes which you will probably do not want to modify):
- wpjobboard/config/frontend-routes.ini – job board routes
- wpjobboard/config/resumes-routes.ini – resumes routes
You can check them out to see how routes are configured, however if you want to modify or add routes then create file:
- wpjobboard/environment/frontend-routes.ini – for job board
- wpjobboard/environment/resumes-routes.ini – for resumes
There are two reasons for this. First when you run automatic update the files in “config” directory will be overwritten and you would loose all your changes, second in case you make an error you can simply delete created route file instead of debugging it.
How routes are defined?
You can say that there are two types of routes, the one that are based only on URL path, and routes that need both URL path and matching row in the database table.
The example of the first category are routes to the add job and index page
[step_add]
pattern = "add/*"
module = "addJob"
action = "add"
[index_page]
pattern = "page/(page)"
module = "index"
action = "index"
param.page = "int"
The most important in both examples is the pattern. You can put there a static string like “post-a-job”, then if Router detects that the URL path is equal to “post-a-job” then job board will display post a job page.
However most of the time you need some variable in the URL, like page number on the list of jobs and in fact the Router allows it. In the pattern variable is a string in parentheses. You also need to define variable type (param.page = “int”) allowed types are “slug”, “string”, “int”.
To sum it up, each request to “page/1″, “page/123″ or “page/643546″ will route to index_page.
The second type of routes are routes that need to match row in the database, for example job details page.
[job]
pattern = "view/(job_slug)"
module = "index"
action = "single"
model = "Wpjb_Model_Job"
param.job_slug = "slug"
The pattern is pretty similar to index_page, however note that there is “model = “Wpjb_Model_Job”" line. It tells the Router that route for example “view/extra-job” will be valid only if in wpjb_job table there is a row with job_slug equal to “extra-job”.
Managing Routes
At the beginning i said that you should create your route files in wpjobboard/environment directory. If you do this then both route files will be loaded and data from wpjobboard/environment will overwrite data from wpjobboard/config. What does this mean? You do not need to copy whole file, you can add or change only the lines you need.
For example if you would like to change pattern of job details routes you can do this by adding to the wpjobboard/environment/frontend-routes.ini following lines:
[job]
pattern = "view/(job_slug).html"