Skip to content

Instantly share code, notes, and snippets.

@2803media
Created September 6, 2016 18:36
Show Gist options
  • Select an option

  • Save 2803media/ebb646e9ad97cf31153c59e848090d6f to your computer and use it in GitHub Desktop.

Select an option

Save 2803media/ebb646e9ad97cf31153c59e848090d6f to your computer and use it in GitHub Desktop.
<?php
class MeteoVille {
/**
* Constructor
*
* @return void
* @author Amaury Balmer
*/
function MeteoVille() {
add_action( 'generate_rewrite_rules', array(&$this, 'rewriteRules') );
add_filter( 'query_vars', array(&$this, 'addQueryVars') );
add_action( 'parse_query', array(&$this, 'parseQuery') );
//add_action( 'init', array(&$this, 'flushRules') );
}
/**
* flush rules
*
* @return void
* @author Amaury Balmer
*/
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
/**
* Rewrite rule for meteo ville
* Example : http://www.blogdecodesign.fr/galerie/day_two_lower_manhattan7/5/4112/4997649173/69244596c2/79835877N00/567/5eacb11935/
*
* @param object $wp_rewrite
* @return void
* @author Amaury Balmer
*/
function rewriteRules( $wp_rewrite ) {
$new_rules = array(
'meteo/([^/]+)-([^/]+)?$' => 'index.php?ville='. $wp_rewrite->preg_index(1).'&CODGEO='. $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
/**
* Add query vars on WP_Query
*
* @param array $public_query_vars
* @return array
* @author Amaury Balmer
*/
function addQueryVars($public_query_vars) {
$public_query_vars[] = 'ville';
$public_query_vars[] = 'CODGEO';
return $public_query_vars;
}
/**
* Check WP_Query for plan ville
*
* @param object $query
* @return void
* @author Amaury Balmer
*/
function parseQuery() {
$page = $_SERVER['REQUEST_URI'];
$page = explode("/", $page);
if (($page[1] == 'meteo')) {
add_action( 'template_redirect', array(&$this, 'includeTemplate') );
}
}
/**
* Load correct templates on theme.
*
* @return void
* @author Amaury Balmer
*/
function includeTemplate() {
$templates[] = "meteo.php";
locate_template( $templates, true );
exit();
}
}
new MeteoVille();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment