How to work with routes in wordpress

Asked

Viewed 993 times

1

It is possible to work with custom routes within wordpress?

I’m building a site that consumes an api where the user can perform product searches.

I would like to customize the site url to determine pages in the following way meusite.com/status/city/neighborhood/aqui_vem_o_meu_product this url would be directed to a wordpress page.

att,

1 answer

2


Are you looking for Rewrite API. It is used to customize Urls so that you can search for values within your query in any way you need.

Example of Codex:

<?php
  function custom_rewrite_rule() {
    /**
     * add_rewrite_tag() cria "tags" que podem ser acessadas pelo 
     * objeto de query padrão, usando get_query_var()
     */
    add_rewrite_tag('%food%', '([^&]+)');
    add_rewrite_tag('%variety%', '([^&]+)');

    /**
     * add_rewrite_rule() cria regras para traduzir as queries em URLs
     * no formato desejado.
     *
     * A regra aqui cria a URL: http://example.com/nutrition/milkshakes/strawberry/
     * transformada em food=milkshakes e variety=strawberry
     */      
    add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);
?>

Links:

add_rewrite_rule()

add_rewrite_tag()

  • Thank you, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.