View Full Version : How to include PHP in a template via Plugins
CoffeeLovesYou
09-22-2012, 12:37 AM
Hi,
Currently, I have this.
ob_start();
include('table.php');
$includedphp = ob_get_contents();
ob_end_clean();
and in my template, it has $includedphp to call the PHP file over.
How can I - instead of using table.php, put the PHP code that is already IN table.php in the actual plugin and still use $includedphp in the template? If someone gets the link to table.php, they will be able to see info I don't want them to see -- the table.php is literally just PHP/MySQL table code, I have it calling it to a template to look nicer and in the template, I have it so only Staff usergroup ids can see it, but I can't make it that way for the table.php, since it isn't part of vBulletin. Hope this was clear. Just trying to put the PHP code inside of the Plugin instead of using include('table.php')
Lynne
09-22-2012, 01:52 AM
Have you tried just copying it into the plugin?
CoffeeLovesYou
09-22-2012, 01:32 PM
Yes but I'm not sure where to put it.
For example, would it be..
ob_start();
include('my
php
code
here');
$includedphp = ob_get_contents();
ob_end_clean();
or would it be like..
ob_start();
my
php
code
here
$includedphp = ob_get_contents();
ob_end_clean();
Can you help me as to where to place it?
Lynne
09-22-2012, 02:40 PM
All you would need is:
php code here
CoffeeLovesYou
09-22-2012, 03:17 PM
how would I attach that to my template, then?
I think it's the second example you posted above, since the code in table.php probably outputs html and you'd still need to capture it, unless you rewrite the code so that the output is saved in a string.
CoffeeLovesYou
09-22-2012, 07:48 PM
I've tried the second example, but I get thrown a compile_template error all over my forum.
I have tried putting the actual PHP code in the template, doesn't work. I have tried including the php file in the plugin then calling it in the template with $includedphp but if someone got the link to the php file in the plugin, they could see the form that only staff are supposed to see. I don't want that.
OK, if this works:
ob_start();
include('table.php');
$includedphp = ob_get_contents();
ob_end_clean();
Then you should be able to do this:
ob_start();
// code from table.php here
$includedphp = ob_get_contents();
ob_end_clean();
but you have to remove <?php from the begininng, and ?> from the end (if it's there).
If what you're saying is that you're worried about someone going directly to table.php, then you could put a check at the beginning of table.php (after the <?php) like:
if (!defined('THIS_SCRIPT')) die("permission denied.");
CoffeeLovesYou
09-23-2012, 03:51 AM
I get this when I try to put the code instead of including the PHP file
Fatal error: Call to undefined function compile_template() in my root forums/global.php(607) : eval()'d code on line 651
The plugin hook is global_start
Hmm...the error looks like the code in table.php is trying to use the function compile_template() and isn't finding it, but I don't see why moving the code from table.php to the plugin would do that. It seems like you should be getting the same error if you include the file like you show in the first post. Also, I'm curious what the code is doing where it would need to call compile_template() - that seems a little strange for a plugin using global_start. That's really an adminCP function (unless it's trying to call a non-vbulletin function of the same name).
CoffeeLovesYou
09-23-2012, 03:07 PM
Hmm...the error looks like the code in table.php is trying to use the function compile_template() and isn't finding it, but I don't see why moving the code from table.php to the plugin would do that. It seems like you should be getting the same error if you include the file like you show in the first post. Also, I'm curious what the code is doing where it would need to call compile_template() - that seems a little strange for a plugin using global_start. That's really an adminCP function (unless it's trying to call a non-vbulletin function of the same name).
I will PM you my table.php
It is not calling compile_template() at all, I think it's the PHP script posted above several times lol
As I mentioned in the PM I just sent you, I think the problem is something I didn't think of - the table.php file has html at the very beginning and the very end, so instead of removing the <?php and ?>, you actually need to add ?> before the code and <?php after the code.
CoffeeLovesYou
09-23-2012, 03:30 PM
As I mentioned in the PM I just sent you, I think the problem is something I didn't think of - the table.php file has html at the very beginning and the very end, so instead of removing the <?php and ?>, you actually need to add ?> before the code and <?php after the code.
Oh wow, that did work!! Thank you so much :)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.