0

I am currently trying to rewrite or restructure a url.. I have three search fields, after fullfiling it when user click search it takes you to oter page and link forms like

"http://goasquare.com/jobs/?keywords=res&categories%5B%5D=44&location=goa&search_region=&submit=&filter_job_type%5B%5D="

i just want to restructure it like below url http://goasquare.com/jobs/%keyword%/%category%/%location%

i have tried many solutions but not workin , please help me

1
  • i have tried code below 'add_action( 'init', 'wpse16819_init' ); function wpse16819_init() { add_rewrite_rule( '/jobs/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?pagename=jobs&keywords=$matches[90]&categories=$matches[91]&location=$matches[92]', 'top' ); } add_filter( 'query_vars', 'wpse16819_query_vars' ); function wpse16819_query_vars( $query_vars ) { $query_vars[] = 'keywords'; $query_vars[] = 'categories'; $query_vars[] = 'location'; return $query_vars; }' Jan 25, 2018 at 18:00

1 Answer 1

0

From what I understand you are after pretty URLs for your form GET sending form. There are two steps involved here.

First you need to prevent the default behaviour of submit form buttons: grabbing the <form> action attribute (e.g. <form action="http://foo.com">) and adding to it the values of the inputs as URL parameters (GET parameters). You need to use JavaScript for this - see this question and its answers.

Now that you have the friendly URL and redirecting to it, you need to "help" WordPress make sense of it. So, secondly, you need to add a new rewrite rule that will map the URL to variables as needed by your server-side logic. This code should get you started (I assumed the slug of your page is jobs):

add_rewrite_rule( '/jobs/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?pagename=jobs&keywords=$matches[1]&categories=$matches[2]&location=$matches[3]', 'top' );

Here is starter JavaScript for you to get going (it is based on jQuery, not vanilla JS):

<script type="text/javascript">
(function($) {
    $(window).load(function(){
        var $form = $("#someform");
        $form.on('submit', function(event) {
          event.preventDefault();
          // Create the new URL
          var new_url = $form.attr('action') + "/" + encodeURIComponent($form.find("input[name=keywords]").first().val()) + "/" + encodeURIComponent($form.find("input[name=categories]").first().val()) + "/" + encodeURIComponent($form.find("input[name=location]").first().val()) + "/";
          // This alert is just to help you see the URL - delete it when no needed
          alert(new_url);

          // Now we will redirect to the URL
          window.location.replace(new_url);

          // We need to return false to stop the normal form submission
          return false;
        });
    });
})(jQuery);
</script>

If you find it easier, here is the jsfiddle for the code above.

8
  • sorry but i am not really good at javascript,, so if u can please help me in this, and i think wil be using add_rewrite_rule on init hook? Jan 25, 2018 at 17:00
  • Yes, the init hook should be fine for adding rewrite rules. Also, don't forget to flush the rewrite rules after you make changes to the code (WP Admin -> Settings -> Permalinks and click Save).
    – Vlad Olaru
    Jan 26, 2018 at 9:14
  • I've updated my answer with the JS starter code.
    – Vlad Olaru
    Jan 26, 2018 at 9:40
  • really thankful for doing this but problem is not resolved yet,,it just not working,,, when the form submit it just make url more messy goasquare.com/jobs/resturant/zoo/… please help me out of this hell problem Jan 26, 2018 at 15:19
  • I have updated my JS because I haven't added the onLoad logic. Simply put the script above inline, near your form, and you should be good to go.
    – Vlad Olaru
    Jan 26, 2018 at 16:14

Your Answer

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

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