How to pass parameters through the URL in Wordpress?

Asked

Viewed 4,165 times

4

I have an event page in Wordpress with the following URL structure http://exemplo.com.br/eventos.

I would like to pass the following parameters, exemplo.com.br/eventos/ano/mês, to list the events of the month and year.
How can I do that?

  • You use something with a . htaccess ?

  • No, I haven’t tried with any rules in htaccess yet

  • Podrías tratar pasando los valores por get...de la forma http://exemplo.com.br/eventos?ano="2014"&mes="Enero"

  • Rober Rozas, I would like to do right in the url, without being via GET, know any method of doing this?

  • 3

    Lee la siguiente Documentación: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/

  • It worked!! Thank you very much Robert Rozas.

  • Excelente Leandro ;)

Show 2 more comments

4 answers

1

WP-Router

What you need is to create specific routes, this plugin helps in this task.

You can create your routes and then recover on some specific page, example:

Whenever the user logs in site.com/eventos/2013/abril you can direct it to the file eventos.php and handle it the way you want, the parameters will be available in the global variable $_GET.

1

You can add something like this to your functions.php.

<?php

    function add_my_var($public_query_vars) {

        $public_query_vars[] = "var_name";

        return $public_query_vars;

    }

    add_filter("query_vars", "add_my_var");

    function do_rewrite() {

        add_rewrite_rule("^page-name/([^/]+)/?$", "index.php?pagename=page-name&var_name=\$matches[1]", 'top');

    }

    add_action("init", "do_rewrite");

?>

In this case the URL would be close to this: http://example.com/page-name/my-param

To recover past value you can both use get_query_var("var_name") how much can you take natively: $_GET["var_name"]

1

Boy, natively, that’s how the Archives list works. For example, I want to see the June 2013 posts from Not Saved:

http://www.naosalvo.com.br/2013/06/

1

Interesting exercise. And following the tip of Robert Rozas (muchas Gracias! ) I arrived at the following code (see comments for detailed usage instructions and other explanations).

In general, just copy the code in a PHP file and put it in the folder /wp-content/plugins.php. Once enabled, you need to update the Permanent Links. After that, create a page-template for the Events page and use the example described in Mode of Use.

<?php
/**
 * Plugin Name: Ano e mês para a página Eventos
 * Plugin URI:  /a/8957/201
 * Description: Adiciona as query vars /ano/mes/ à URL da página Eventos. Escrito com base no artigo publicado no blog rlmseo.com 
 * Author:      brasofilo
 * License:     GPLv3

 MODO DE USO 
 - Após ativar o plugin, atualize a configuração de Links Permanentes, 
   visitando /wp-admin/options-permalink.php e clicando em "Salvar alterações"
 - Crie um Template para a página "Eventos" 
   e certifique-se que o slug da página é "eventos", ie, exemplo.com/eventos
 - Na page template (page-eventos.php) do theme, utilize
        if( isset( $wp_query->query_vars['ano'] ) && isset( $wp_query->query_vars['mes'] ) ) 
        {
            $ano = urldecode( $wp_query->query_vars['ano'] );
            $mes = urldecode( $wp_query->query_vars['mes'] );
            printf( '<h2>Ano: %s</h2>', $ano );
            printf( '<h2>Mes: %s</h2>', $mes );
        }
        // ou use $ano = get_query_var('ano');
 - Visite a página Eventos no site usando a URL exemplo.com/eventos/teste-ano/teste-mes/
 - Agora é só usar as variáveis que vieram da URL, $ano e $mes
 */

add_filter( 'query_vars', 'add_query_vars' );
add_filter( 'rewrite_rules_array', 'add_rewrite_rules' );

/**
 * Adiciona 'ano' e 'mes' à lista de query vars registradas
 *
 * @param     $vars array
 * @return    array
 */
function add_query_vars( $vars ) 
{
    array_push( $vars, 'ano', 'mes' );
    return $vars;
}

/**
 * Adiciona a regra de rewrite ao banco de dados
 *
 * @param     $rules array
 * @return    array
 */
function add_rewrite_rules( $rules ) 
{
    $new_rules = array_merge( $rules, array( 
        'eventos/(.+?)/(.+?)/?$' => 'index.php?pagename=eventos&ano=$matches[1]&mes=$matches[2]' 
    ));
    return $new_rules;
}

Another implementation can be seen in Receive a parameter through a friendly URL on a Wordpress page

Why use a plugin? See: Where to put my code: plugin or functions.php?

Browser other questions tagged

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