I'm playing around with a custom theme for Wordpress and I have this code snippet in my header, home and footer.php files.
<?php
//Get the Wordpress page_id and language for use in template items.
global $pg_id;
global $lang;
if ($_GET["page_id"] && is_numeric($_GET["page_id"])) {
$pg_id = $_GET["page_id"];
}
$validLang = array("en", "fr");
if ($_GET["lang"] && in_array($_GET["lang"], $validLang)) {
$lang = $_GET["lang"];
} else {
$lang = "en";
}
//echo ($lang);
//echo ($pg_id);
?>
I should have this code in one location. Where should I place it so the global variable $lang is available in the header, footer, home and other template files?
I couldn't get the function working properly in functions.php (although I do think that's a possible location to place the code). I ended up using a php include: http://php.net/manual/en/function.include.php
Place it in the functions.php
file of your theme, that is for all the shared code and function of the theme.
This file is loaded during wordpress installation and the functions defined here are visible both to the admin and the theme.
See here for a deeper explaination.