View Full Version : using PHP in templates?
halliday06
07-22-2008, 07:41 PM
I'm starting to integrate the forum into my site, in the HEADER template i added the code for my navigation which is betwen <?php ?> tags, it just shows the code on the actual forum page
i have tried removing the tags incase it was already a php template but it didnt work either,
any ideas?
Dylanblitz
07-22-2008, 07:56 PM
I'm starting to integrate the forum into my site, in the HEADER template i added the code for my navigation which is betwen <?php ?> tags, it just shows the code on the actual forum page
i have tried removing the tags incase it was already a php template but it didnt work either,
any ideas?
The templates don't process the php because they are not meant to have php in them. All the php processing is designed to be done in the php files.
What I would do is search for the php file that calls that template (I think it's global.php). Put your php in that file before it calls the template, assign the output to a variable and then put that variable in the place you want it to show in the template.
Opserty
07-22-2008, 08:00 PM
No need to edit PHP files. You can place PHP in a Plugin (AdminCP > Plugins & Products > Add New Plugin). Assign any output to a specific variable and use that variable in the template.
Dylanblitz
07-22-2008, 08:03 PM
No need to edit PHP files. You can place PHP in a Plugin (AdminCP > Plugins & Products > Add New Plugin). Assign any output to a specific variable and use that variable in the template.
Duh, spaced on the plugin system for some reason :)
halliday06
07-22-2008, 08:09 PM
thanks..
what would hook location be if i want it in the HEADER template.
and what would the hook be? it doesnt seem to have field to set it
Opserty
07-22-2008, 08:12 PM
Hook location: global_start or global_setup_complete
halliday06
07-22-2008, 08:14 PM
and then i would put $global_start
where i wanted it to appear???. sorry, new to this
RLShare
07-22-2008, 09:14 PM
and then i would put $global_start
where i wanted it to appear???. sorry, new to this
No you would create variable in the plug-in and fill it with data
$myvariable= "<strong> THIS IS MY VARIABLE DATA </strong>";
Then in your template add $myvariable
halliday06
07-22-2008, 09:26 PM
hmm its not working for me. nothing appears
i chose global_start for hook location,execution order: 5
i put the variable $alnav in my template where i wanted it to appear
this is sort of the code i am using:
any ideas?
$alnav = "if (empty($p)) {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"home\" /></a></li>\";
} else {
if ($p == 'home') {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"\home\" /></a></li>\";
} else {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_l.gif\" id=\"\home\" /></a></li>\";
}
}
if ($p == 'forums') {
echo \"<li><a href=\"/forums\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('forums','','images/nav/nav_forums_h.gif',1)\">
<img src=\"images/nav/nav_forums_a.gif\" id=\"forums\" /></a></li>\";
} else {
echo \"<li><a href=\"/forums\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('forums','','images/nav/nav_forums_h.gif',1)\">
<img src=\"images/nav/nav_forums_l.gif\" id=\"forums\" /></a></li>\";
}
if ($vbulletin->userinfo['userid']!=0) {
$userid = $vbulletin->userinfo['userid'];
if ($p == 'profile') {
echo \"<li><a href=\"forums/member.php?u=" . $userid . "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('profile','','images/nav/nav_profile_h.gif',1)\">
<img src=\"images/nav/nav_profile_a.gif\" id=\"profile\" /></a></li>";
} else {
echo "<li><a href=\"forums/member.php?u=" . $userid . "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('profile','','images/nav/nav_profile_h.gif',1)\">
<img src=\"images/nav/nav_profile_l.gif\" id=\"profile\" /></a></li>";
}
} else {
if ($p == 'register') {
echo "<li><a href=\"forums/register.php\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('register','','images/nav/nav_register_h.gif',1)\"><img src=\"images/nav/nav_register_a.gif\" id=\"register\" /></a></li>";
} else {
echo "<li><a href=\"forums/register.php\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('register','','images/nav/nav_register_h.gif',1)\"><img src=\"images/nav/nav_register_l.gif\" id=\"register\" /></a></li>";
}
if ($p == 'login') {
echo "<li><a href=\"?p=login\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('login','','images/nav/nav_login_h.gif',1)\"><img src=\"images/nav/nav_login_a.gif\" id=\"login\" /></a></li>";
} else {
echo "<li><a href=\"?p=login\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('login','','images/nav/nav_login_h.gif',1)\"><img src=\"images/nav/nav_login_l.gif\" id=\"login\" /></a></li>";
}
}
if ($p == 'downloads') {
echo "<li><a href=\"?p=downloads\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('downloads','','images/nav/nav_downloads_h.gif',1)\">
<img src=\"images/nav/nav_downloads_a.gif\" id=\"downloads\" /></a></li>";
} else {
echo "<li><a href=\"?p=downloads\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('downloads','','images/nav/nav_downloads_h.gif',1)\">
<img src=\"images/nav/nav_downloads_l.gif\" id=\"downloads\" /></a></li>";
}
if ($p == 'contact') {
echo "<li><a href=\"forums/sendmessage.php\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('contact','','images/nav/nav_contact_h.gif',1)\">
<img src=\"images/nav/nav_contact_a.gif\" id=\"contact\" /></a></li>";
} else {
echo "<li><a href=\"forums/sendmessage.php\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('contact','','images/nav/nav_contact_h.gif',1)\">
<img src=\"images/nav/nav_contact_l.gif\" id=\"contact\" /></a></li>";
}
if ($p == 'links') {
echo "<li><a href=\"?p=links\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('links','','images/nav/nav_links_h.gif',1)\">
<img src=\"images/nav/nav_links_a.gif\" id=\"links\" /></a></li>";
} else {
echo "<li><a href=\"?p=links\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('links','','images/nav/nav_links_h.gif',1)\">
<img src=\"images/nav/nav_links_l.gif\" id=\"links\" /></a></li>";
}
if ($p == 'about') {
echo "<li><a href=\"?p=about\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('about','','images/nav/nav_about_h.gif',1)\">
<img src=\"images/nav/nav_about_a.gif\" id=\"about\" /></a></li>";
} else {
echo "<li><a href=\"?p=about\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('about','','images/nav/nav_about_h.gif',1)\">
<img src=\"images/nav/nav_about_l.gif\" id=\"about\" /></a></li>";
}
if ($vbulletin->userinfo['userid']!=0) {
if ($p == 'logout') {
echo "<li><a href=\"".$forumpath."login.php?$session[sessionurl]do=logout&logouthash=$logouthash";
echo $vbulletin->userinfo['logouthash'];
echo "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('logout','','images/nav/nav_logout_h.gif',1)\"><img src=\"images/nav/nav_logout_a.gif\" id=\"logout\" /></a></li>";
} else {
echo "<li><a href=\"".$forumpath."login.php?$session[sessionurl]do=logout&logouthash=$logouthash";
echo $vbulletin->userinfo['logouthash'];
echo "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('logout','','images/nav/nav_logout_h.gif',1)\"><img src=\"images/nav/nav_logout_l.gif\" id=\"logout\" /></a></li>";
}
}";
Dylanblitz
07-22-2008, 09:38 PM
hmm its not working for me. nothing appears
i chose global_start for hook location,execution order: 5
i put the variable $alnav in my template where i wanted it to appear
this is sort of the code i am using:
any ideas?
$alnav = "if (empty($p)) {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"home\" /></a></li>\";
} else {
if ($p == 'home') {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"\home\" /></a></li>\";
} else {
echo \"<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_l.gif\" id=\"\home\" /></a></li>\";
}
}
if ($p == 'forums') {
echo \"<li><a href=\"/forums\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('forums','','images/nav/nav_forums_h.gif',1)\">
<img src=\"images/nav/nav_forums_a.gif\" id=\"forums\" /></a></li>\";
} else {
echo \"<li><a href=\"/forums\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('forums','','images/nav/nav_forums_h.gif',1)\">
<img src=\"images/nav/nav_forums_l.gif\" id=\"forums\" /></a></li>\";
}
if ($vbulletin->userinfo['userid']!=0) {
$userid = $vbulletin->userinfo['userid'];
if ($p == 'profile') {
echo \"<li><a href=\"forums/member.php?u=" . $userid . "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('profile','','images/nav/nav_profile_h.gif',1)\">
<img src=\"images/nav/nav_profile_a.gif\" id=\"profile\" /></a></li>";
} else {
echo "<li><a href=\"forums/member.php?u=" . $userid . "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('profile','','images/nav/nav_profile_h.gif',1)\">
<img src=\"images/nav/nav_profile_l.gif\" id=\"profile\" /></a></li>";
}
} else {
if ($p == 'register') {
echo "<li><a href=\"forums/register.php\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('register','','images/nav/nav_register_h.gif',1)\"><img src=\"images/nav/nav_register_a.gif\" id=\"register\" /></a></li>";
} else {
echo "<li><a href=\"forums/register.php\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('register','','images/nav/nav_register_h.gif',1)\"><img src=\"images/nav/nav_register_l.gif\" id=\"register\" /></a></li>";
}
if ($p == 'login') {
echo "<li><a href=\"?p=login\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('login','','images/nav/nav_login_h.gif',1)\"><img src=\"images/nav/nav_login_a.gif\" id=\"login\" /></a></li>";
} else {
echo "<li><a href=\"?p=login\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('login','','images/nav/nav_login_h.gif',1)\"><img src=\"images/nav/nav_login_l.gif\" id=\"login\" /></a></li>";
}
}
if ($p == 'downloads') {
echo "<li><a href=\"?p=downloads\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('downloads','','images/nav/nav_downloads_h.gif',1)\">
<img src=\"images/nav/nav_downloads_a.gif\" id=\"downloads\" /></a></li>";
} else {
echo "<li><a href=\"?p=downloads\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('downloads','','images/nav/nav_downloads_h.gif',1)\">
<img src=\"images/nav/nav_downloads_l.gif\" id=\"downloads\" /></a></li>";
}
if ($p == 'contact') {
echo "<li><a href=\"forums/sendmessage.php\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('contact','','images/nav/nav_contact_h.gif',1)\">
<img src=\"images/nav/nav_contact_a.gif\" id=\"contact\" /></a></li>";
} else {
echo "<li><a href=\"forums/sendmessage.php\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('contact','','images/nav/nav_contact_h.gif',1)\">
<img src=\"images/nav/nav_contact_l.gif\" id=\"contact\" /></a></li>";
}
if ($p == 'links') {
echo "<li><a href=\"?p=links\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('links','','images/nav/nav_links_h.gif',1)\">
<img src=\"images/nav/nav_links_a.gif\" id=\"links\" /></a></li>";
} else {
echo "<li><a href=\"?p=links\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('links','','images/nav/nav_links_h.gif',1)\">
<img src=\"images/nav/nav_links_l.gif\" id=\"links\" /></a></li>";
}
if ($p == 'about') {
echo "<li><a href=\"?p=about\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('about','','images/nav/nav_about_h.gif',1)\">
<img src=\"images/nav/nav_about_a.gif\" id=\"about\" /></a></li>";
} else {
echo "<li><a href=\"?p=about\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('about','','images/nav/nav_about_h.gif',1)\">
<img src=\"images/nav/nav_about_l.gif\" id=\"about\" /></a></li>";
}
if ($vbulletin->userinfo['userid']!=0) {
if ($p == 'logout') {
echo "<li><a href=\"".$forumpath."login.php?$session[sessionurl]do=logout&logouthash=$logouthash";
echo $vbulletin->userinfo['logouthash'];
echo "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('logout','','images/nav/nav_logout_h.gif',1)\"><img src=\"images/nav/nav_logout_a.gif\" id=\"logout\" /></a></li>";
} else {
echo "<li><a href=\"".$forumpath."login.php?$session[sessionurl]do=logout&logouthash=$logouthash";
echo $vbulletin->userinfo['logouthash'];
echo "\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('logout','','images/nav/nav_logout_h.gif',1)\"><img src=\"images/nav/nav_logout_l.gif\" id=\"logout\" /></a></li>";
}
}";
How your doing it is a bit off. What you want to be in the $alnav is the end result code you want shown on the page. So it would be something like:
unset($alnav);
if (empty($p)) {
$alnav .= "<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"home\" /></a></li>";
} else {
if ($p == 'home') {
$alnav .= "<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_a.gif\" id=\"\home\" /></a></li>";
} else {
$alnav .= "<li><a href=\"../?p=home\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('home','','../images/nav/nav_home_h.gif',1)\">
<img src=\"../images/nav/nav_home_l.gif\" id=\"\home\" /></a></li>";
}
}
RLShare
07-22-2008, 09:41 PM
You got it a little confused. You use PHP directly in the plug-in and then place the HTML you want into a variable to be displayed in the template...
Here is a quick example of what a plugin would look like with your first if statement....
$alnav='';
if (empty($p)) {
$alnav.='<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'home\',\'\',\'../images/nav/nav_home_h.gif\',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
}
Then you would place a reference to $alnav into the template
Opserty
07-22-2008, 10:57 PM
Probably the easiest thing to do is to use output buffering.
E.g.
ob_start();
// Your original PHP here... not the one you've edited
$somevar = ob_get_contents();
ob_end_clean();
Use $somevar in your template
halliday06
07-23-2008, 07:03 AM
thanks for the help i'll give it a go
--------------- Added 1216814486 at 1216814486 ---------------
as a test i ran this:
nothing shows... and whenever i enable the plugin i get this error:
Fatal error: Call to a member function on a non-object in /home/forcer/public_html/forums/includes/functions_forumdisplay.php on line 182
thats with the hook global_start, when i use the hook global_setup_complete i get an error at the top of the page.
$alnav='';
if (empty($p)) {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
} else {
if ($p == 'home') {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
} else {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_l.gif" id="\home" /></a></li>';
}
}
Dylanblitz
07-23-2008, 03:09 PM
If you are using
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
then you need to put the \ before any 's in there
like
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'home\',\'\',\'../images/nav/nav_home_h.gif\',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
RLShare
07-23-2008, 04:49 PM
$alnav='';
if (empty($p)) {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
} else {
if ($p == 'home') {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_a.gif" id="home" /></a></li>';
} else {
$alnav .= '<li><a href="../?p=home" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','../images/nav/nav_home_h.gif',1)">
<img src="../images/nav/nav_home_l.gif" id="\home" /></a></li>';
}
}
Besides what was said one post above me, that code their would go directly into a plug-in without the output buffering OP mentioned. If your using output buffering nothing will show up. Output buffering is for times when code executes and echos or prints data instead of storing it in a variable, it allows you to take what would be outputed directly to the browser and store it. Like your original post that has code, if you took out the variable and quotes, all the code you have in between would need output buffering.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.