Update: Code has been updated to read the database file from EE’s config file.
Task at Hand:
You want to use a template Global Variable in your Forums.
You discovered the hard way that the Forums has its own templating system and you are now tweaking the Forum templates. Instead of recreating the content of a Global Variable into your Forum templates, you simply want to use the actual site Global Variable inside the Forums templates.
Good idea. This way you only have to edit the Global Variable in one place.
Then you discover that you cannot use a Global Variable outside the Site’s Templates and in your Forums.
Background:
Global Variables are meant to be used only in the Site Templates because only the site template parsing can parse the Global Variables. Remember that Global Variables can contain only static content (per EE’s guidelines at http://expressionengine.com/docs/templates/globals/user_defined.html) which may fool you into thinking that they can be used in the Forums. However, the Forums template parsing cannot “read” (aka parse) the site template Global Variables.
Possible Solution:
Since we cannot access a Global Variable via EE from the Forums, lets retrieve the contents of the Global Variable by going directly to the database.
Example Scenario:
You have a template Global Variable for a menu which you would also like to use in the Forums templates.
Steps:
1) Identify which Global Variables you want to use. In my example scenario, I want to use two variables called mainmenu and footermenu. Each has static content.
2) In the Forum templates, edit the desired template to where you want to use your Global Variable.
In my case, I edited Global Templates > HTML Header and Global Templates > HTML Footer.
Place this in the desired spot or spots if you want to use more than one Global Variable. Just change $globalvariable in each instance:
<?php
$globalvariable="mainmenu"; // template Global Variable to be passed to the php script
include("themes/forum_themes/themename/scripts/menu.php");
?>
3) Create menu.php in the above directory using the code below. Obviously, you will need either direct access to the server or FTP to your web host.
<?php
global $PREFS;
// pull database info from config file
$dbname = $PREFS->core_ini['db_name'];
$hostname = $PREFS->core_ini['db_hostname'];
$dbuser = $PREFS->core_ini['db_username'];
$dbpass = $PREFS->core_ini['db_password'];
// query the database
$dbhandle = mysql_connect($hostname, $dbuser, $dbpass) or die("Connection Failure to Database");
mysql_select_db($dbname, $dbhandle) or die ($dbname . " Database not found. " . $dbuser);
$query = "SELECT variable_data FROM exp_global_variables WHERE variable_name='". $global_variable . "'";
$result = mysql_db_query($dbname, $query) or die("Failed Query of: " . $query);
$thisrow = mysql_fetch_row($result);
if ($thisrow) //if the results of the query are not null
{
// output the results
echo $thisrow[0]; // there should only be one row returned
}
?>Recommended placement of menu.php is in your themes directory in a separate folder off the root; otherwise, you may notice some odd behavior in the Control Panel.
You can optionally ignore the initial code in Step 2 and instead put the code above directly in your Forum templates. But in the example scenario, I have a Main Menu and a Footer Menu and did not want to duplicate the code.
Explanation:
The result should be that the Global Variable’s contents will be parsed out as static content in the Forum templates.
Since we cannot retrieve the desired Global Variable via EE, we are going directly to the database and reading the contents of the variable.
—
This method can probably be fine tuned but is my attempt to solve a situation I ran into.
There are other scenarios on why you would want to use a Global Variable but at least this should allow you to access the Global Variable’s content.