How to pass parameters by URL in WordPress?

3

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 this?

    
asked by anonymous 18.02.2014 / 19:11

4 answers

1

Interesting exercise. And following the tip of Robert Rozas (Muchas gracias!) I got to the following code (see comments for instructions detailed usage and other explanations).

In general, just copy the code into a PHP file and put it in the /wp-content/plugins.php folder. Once activated, it is necessary to update the Permanent Links. After that, create a page-template for the Events page and use the example described in Usage Mode .

<?php
/**
 * Plugin Name: Ano e mês para a página Eventos
 * Plugin URI:  https://pt.stackoverflow.com/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 Receiving a parameter through a friendly URL in a Wordpress page

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

11.03.2014 / 21:42
0

Boy, natively, already works this way the listing of Archives. For example, I want to see the June 2013 posts of not Saved:

http://www.naosalvo.com.br/2013/06/
    
24.02.2014 / 15:26
0

WP-Router

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

You can create your routes and then retrieve them on a specific page, for example:

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

    
10.03.2014 / 16:29
0

You can add something like this in 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 the past value you can either use get_query_var("var_name") or natively: $_GET["var_name"]

    
10.03.2014 / 18:14