MyBB Community Forums

Full Version: Adding PHP to templates using global.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Disclaimer: I'm brand new to MyBB and have spent the last 2 hours looking for a solution to this problem but came up empty, hence the post here.

Hi guys,

I want to integrate MyBB into my site using my custom header.php and footer.php files, which output my site's navigation and footer, respectively. Just to give you an idea, here's the header.php code:

<div class="canvas">

<?php include($_SERVER['DOCUMENT_ROOT'].'/include/masthead.php') ?>

<div class="maincontent">

<div style="clear:both"></div>

When I tried to edited my theme's "header" template with this, I learned that MyBB doesn't allow PHP in template files. After much searching, I found this thread, which I thought would solve my problem, but after following the instructions it didn't work, and it looks others have pointed out that this was written for an older version of MyBB and doesn't work anymore. SO...

Could someone please explain to me (keeping in mind I'm a MyBB newb) how I can pull my header.php code into my theme's header template? If the answer is to declare a variable in global.php and then add it to my header template, can you please explain how to do that? I found a couple plugins that allow arbitrary PHP, but they don't sound very secure, so I'd prefer to do it the "right" way if possible.

Thank you for your help!
Does your include/masthead.php have any dynamic elements or is it just plain html?

If it doesn't have any dynamic elements and just plain html then you can just copy/paste the contents of that file in to the header templates.
Hi G33k,

It doesn't have any dynamic elements, but I like using the php include because that way I only have to make changes in one place if I want to change the header. Also, I'm planning on implementing some features in the near future that will require dynamic content in the header.

Isn't there some way to have php in the templates? The thread I linked to above said you could add the php to global.php, store it as a variable, and then call it from the template. The instructions in that thread didn't work (I think because they were for an old version of MyBB), but isn't there still some way to do this?

Thanks,

Chris

Without knowing what exactly you have in your include file here are a few suggestions:

1. Install the php in templates plugin, with this you can directly include the php file in the templates using the php calls. This way would be easiest as you don't have to make any code edits.

2. You could add $foo = include($_SERVER['DOCUMENT_ROOT'].'/include/masthead.php'); in global.php before the header evals and then use {$foo} in the templates to call the included stuff. HOWEVER, for you to do this your included file must return the contents of what you want to include in the templates or else it will just return a 1. Also you'd have to make this edit everytime you update MyBB.
Thanks, G33k. It sounds like you're saying option (1) is the best way to go. I originally shied away from the php in templates plugin because it sounded like it had some security holes, but it seems like a lot of MyBBers use it, and if you're recommending it I'll give it a go.

I'll also get into the core files tomorrow and take a crack at your suggestion for (2), just to see if I can get it to work. But again, it sounds like you're saying (1) is the way to go.

Thanks again.
I'm not recommending the plugin, I am simply suggesting it for your need as it will be easier than manually coding it. I have personally not used the plugin and if you have any concerns about security it is best you take it up with the plugin's author.

If you decide to go the manual route and need help, just PM me.
You may want to look into the PluginLibrary and Patches Plugin. this would allow you to make the core edits (option 2 from -G33K- ) via a plugin that can be easily reapplied after a MyBB update.

It would be a performance improvement over PHP in templates.
Not used this extensively, but you could do:

{${include('header.php')}}

Thanks, guys. Looking into all this stuff now.
(2011-03-29, 03:38 PM)Malcolm. Wrote: [ -> ]Not used this extensively, but you could do:

{${include('header.php')}}

I totally forgot you could do this since it is after all eval'd code. Thanks for that Malcolm.

@OP: This would be the easiest and the most convenient solution for you. No messing around with code or plugins Smile

So you'll have this:

<div class="canvas">

{${include('/include/masthead.php')}}

<div class="maincontent">

<div style="clear:both"></div>

Pages: 1 2