Is dynamic URL possible in WordPress

The question:

I know the title seems little bit confusing, but what I am looking for is something possible in symfony or ror. I have a page with this URL: http://mydomain.com/job/. Now I want this URL structure http://mydomain.com/job/php-developer/45, http://mydomain.com/job/java-developer/46. Now the parameters after /job/ are dynamic, and they are not any page or post, they fall under a page job

Is this possible in WordPress?

UPDATED QUESTION:

Ok as per @Gioia’s answer I updated my code.

Below is my code:

add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
    $aVars[] = "job_title"; 
    $aVars[] = "job_id"; 
    return $aVars;
}

add_action( 'init', 'add_rules' );  
function add_rules() {
    add_rewrite_rule('^/job/([^/]*)/([^/]*)/?','index.php?page_id=13338&job_title=$matches[1]&job_id=$matches[2]','top');
}

So now this is throwing me 404 page when I try this URL http://mydomain.com/job/php-dev/45. But when I try this URL http://mydomain.com/index.php?page_id=13338&job_title=php-dev&job_id=45 it works.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Not completely sure I understand what you mean with that the parameters fall under a page job, but if you mean that different contents are loaded on the same page using javascript / ajax, you could use https://github.com/browserstate/history.js/ to generate the corresponding url for each state. Difficult to say more about how you could implement this without knowing more about what you are trying to do.


UPDATE

I think you should use custom rewrite rules. That is what wordpress uses to create the nice urls, and it has an API to add your own.

First you need to add the tags you need to track, in your case title and job_id. Actually you should probably change title to something like job_title, I am not completely sure title would create a problem, but since it is something in wordpress, it is better to be on the safe side.

I used the following code, you need to add it to your function.php, in your theme.

add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
    $aVars[] = "job_title"; 
    $aVars[] = "job_id"; 
    return $aVars;
}  

More info: http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

Then you add the rewrite rule, also in functions.php:

add_action( 'init', 'add_rules' );  
function add_rules() {
    add_rewrite_rule('^job/([^/]*)/([^/]*)/?','index.php?page_id=12&job_title=$matches[1]&job_id=$matches[2]','top');
}

You should replace the page_id by the id of the job page
More info: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule.

Once you have saved the file, you need to go to settings -> permalinks and simply save without changing anything. That will make sure your settings are correctly loaded.

You can install the plugin rewrite inspector to see all the rewrite rules applied and check that yours are present.
https://wordpress.org/plugins/rewrite-rules-inspector/

You can install the plugin debug bar to inspect the page and see what rewrite rule is being applied to the page:
https://wordpress.org/plugins/debug-bar/

And here you have more info on rewrite rules in general:
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/


UPDATE 2

To retrieve the parameters:

if (isset($wp_query->query_vars['job_title'])) {
    $job_title = urldecode($wp_query->query_vars['job_title']);?>
<?php }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment