2

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.

7
  • You could achieve /job/job-title/ by using a custom post type and possibly /45 by doing a custom structure with your post type.
    – Howdy_McGee
    Commented Jun 18, 2014 at 13:42
  • my URL are dynamic, /job/php-developer/45 , /job/java-developer/46 etc Commented Jun 18, 2014 at 14:21
  • What is the purpose of 45 / 46? Are these counters? What do they represent to make them dynamic?
    – Howdy_McGee
    Commented Jun 18, 2014 at 14:41
  • My goal is to achieve this URL http://mydomain.com/job/?title=php-developer&job_id=45, but to make this URL SEO friendly, I want something like this http://mydomain.com/job/php-developer/45 Commented Jun 18, 2014 at 14:52
  • Where Job ID is a meta value of some kind? That is not a simple task.
    – Howdy_McGee
    Commented Jun 18, 2014 at 15:05

1 Answer 1

5

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 }
8
  • My goal is to achieve this URL http://mydomain.com/job/?title=php-developer&job_id=45, but to make this URL SEO friendly, I want something like this http://mydomain.com/job/php-developer/45 Commented Jun 18, 2014 at 14:47
  • You could use custom rewrite rules: Commented Jun 18, 2014 at 17:38
  • Hi, thanks for your reply. I tried your code, but its throwing 404 page, please check my edit above in question. Commented Jun 19, 2014 at 7:07
  • Hi, I just tried the code and it worked for me. First, check that you have changed the id of the page that came in the code (?page_id=12) to the id of the job page (you can see what is the id of the page when you are in the edit page, it is part of the url: post.php?post=6&action=edit, 6 is the id of the page). Also make sure after every change to go to the permalinks settings page and saving. Commented Jun 19, 2014 at 7:36
  • I changed the page ID as per mine, if I hit this URL index.php?page_id=13338&job_title=$matches[1]&job_id=$matches[2] it is working, but with the custom URL its not. Do I need to add add_rewrite_tag? or some htaccess modifications? Commented Jun 19, 2014 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.