How do I include PHP?

SgtSpike

Member
I would use the search, but can't find anything useful since php is a 3 letter word. :cautious:

Anyway, this stems from XenPorta. Since the author claims anything in the CData tags acts just like regular template code, I just need to know how I would include a php file in regular template code. The most frustrating part is that I know I've seen how to do it on here before, but I lost the thread with that information and I can't search it because of the dang 4 letter limitation on searches!
 
I've decided to instead put the PHP data in the sidebar. I'll be more specific and give examples of the code.
Here's the php file I would like included in the sidebar (will change formatting once I get it working): http://www.minecraftcc.com/players.php

The website: http://www.minecraftcc.com/

I don't know XML at all, so still learning my way around that. The example in the link didn't really answer my question, since it involves pages, but I tried to mess around with it anyway. Here's what I have currently.

I have a copy of players.php in <xenforo>/library/XenForo/Pages/players.php
I created another file called "playerhandler.php" in the same folder. Here's the contents of that file:

PHP:
<?php
class XenForo_Pages_players
{
    public static function includeFile(XenForo_ControllerPublic_Abstract  $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        include('players.php');
    }
}
?>

Then in the template file called forum_list, I inserted the following just above </xen:sidebar>
HTML:
        <div>
                {xen:raw $playerhandler}
        </div>

Nothing shows up in the sidebar. I'm obviously doing something wrong, but XML and the XenForo API is still confusing me enough to the point of I have no clue what I'm doing. :p
 
Arik, can't watch the video at the moment. Am I just completely off base then?

Gordy, I have no idea what a controller is... will try to do some reading if I can find it.

EDIT:

Gordy, interestingly enough, you seem to have been asking the same question a few months back.
http://xenforo.com/community/threads/can-you-do-a-php-include-in-a-template.7628/#post-108418

:)

So, ob_start()? I really have no idea how to make use of ob_start(). Still don't know exactly what a controller is or does or how I use it... I just copy/pasted code from someone else's setup where they were trying to achieve something similar, but on pages, not in a template. I edited the things that seemed like they needed edited, and gave it a try. No dice, so I need to know where to go from here.

It would be AWESOME if someone who is good at development in xenForo could just walk through, step by step, how to include a php file inside a template. All these referrals to documentation at php.net and around here are fine, but it is so piecemeal that it is difficult for someone who is not familiar with xenForo development to catch on. And I see that this is a very common request, yet none of the threads seem to get resolved with an answer on how to do it.

So, anyone want to write a quick tutorial on how to include php files in a template? I would greatly appreciate it, and you'd probably save many other people from asking the same question down the road. ;)
 
Arik, can't watch the video at the moment. Am I just completely off base then?

Gordy, I have no idea what a controller is... will try to do some reading if I can find it.

EDIT:

Gordy, interestingly enough, you seem to have been asking the same question a few months back.
http://xenforo.com/community/threads/can-you-do-a-php-include-in-a-template.7628/#post-108418

:)

So, ob_start()? I really have no idea how to make use of ob_start(). Still don't know exactly what a controller is or does or how I use it... I just copy/pasted code from someone else's setup where they were trying to achieve something similar, but on pages, not in a template. I edited the things that seemed like they needed edited, and gave it a try. No dice, so I need to know where to go from here.

It would be AWESOME if someone who is good at development in xenForo could just walk through, step by step, how to include a php file inside a template. All these referrals to documentation at php.net and around here are fine, but it is so piecemeal that it is difficult for someone who is not familiar with xenForo development to catch on. And I see that this is a very common request, yet none of the threads seem to get resolved with an answer on how to do it.

So, anyone want to write a quick tutorial on how to include php files in a template? I would greatly appreciate it, and you'd probably save many other people from asking the same question down the road. ;)

Would be VERY awesome –

I've tried everything under the sun to get it to work and there has been bits and pieces here and there, videos are nice but you can't copy and paste from them, my posts for help get shuffled around to seemingly inactive, low particpating forums, etc , etc..

I just want to get xf to match my existing site by simply including a 100% wide header.php file that all my other pages in my site use :(

I'll probably just go with iframes
 
Arik, can't watch the video at the moment. Am I just completely off base then?

Almost completely. ;)

Gordy, I have no idea what a controller is... will try to do some reading if I can find it.

The "Controller" is comprised of a bunch of files that have actionXXX methods. Think of those methods as individual PHP pages. They call in models & helpers for getting information, data writers for pushing it to the database. They prepare data, and when they're done, they send it off to the view.

It would be AWESOME if someone who is good at development in xenForo could just walk through, step by step, how to include a php file inside a template. All these referrals to documentation at php.net and around here are fine, but it is so piecemeal that it is difficult for someone who is not familiar with xenForo development to catch on. And I see that this is a very common request, yet none of the threads seem to get resolved with an answer on how to do it.

That's because you can't. Not directly, that is...

Here's the dirty version for what you're trying to do.

PHP:
<?php
class Asp_PhpIncludeTip_Plugin_Plugin
{
    public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'forum_list_sidebar')
        {
            ob_start();
            require_once('php_include.php');
            $contents .= ob_get_contents();
            ob_end_clean();
        }
    }
}

Step by step:

0. Put your board into debug mode. :)
0.5. Create a new Plugin for this.

1. Create a new directory for your plugin. That directory defines your classname (it's how the autoloader works). So, in my case above, that directory is library/Asp/PhpIncludeTip/Plugin/ (library is already there...it's the parent folder for the XenForo folder)

2. Create a file within that directory for your plugin. That's the last part of the class name (the last Plugin in this case). So, as above, that file is going to be Plugin.php

So, Asp_PhpIncludeTip_Plugin_Plugin is translated to Asp/PhpIncludeTip/Plugin/Plugin.php

3. Within the Plugin.php file, set up your class as above, and then include a single method (public static function). At this point, go to the "Code Event Listeners" section of the admin and add a new code event listener (admin.php?code-event-listeners/add). Choose "template_hook". The section will expand to show you more information about the event listener. There's a section there for the "callback signature". Copy that and paste it between the parenthesis. The template hook signature looks like this:

PHP:
$hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template

4. Tell the method what to do. The PHP you type in here gets called on EVERY template hook that is parsed as the code is executed, so one of the first things you're going to want to do is tell it which hook to deal with.

PHP:
if($hookName == 'forum_list_sidebar')

5. Finish telling it what to do, then save out your work. Within the admin, finish setting up the event listener by telling it what class to execute, and what method to call within that class.

My "php include" file is saved in the same directory. If that's not where you want yours, you can throw it wherever. Just make sure you give it the right path (as you would have to regardless...)

Once you save everything out, you should be good to go.

It's Friday, and I'm in a good mood, so I've attached my (working for me!) example.
 

Attachments

Awesome Arik, thanks for taking the time to explain it in more detail for us newbies! :P I will also give it a try here shortly.
 
Got it working! Thanks again so much! A couple of notes...

Enable debug mode by adding $config['debug'] = true; to the library/config.php file.
The event listener is caps-sensitive to your filename. :P
 
Reviving this thread, because I have a problem.

I switched hosts, and most everything went ok. One thing that did not go ok is the custom PHP I had written for the sidebar. For some reason, it reverted to an older version when I switched hosts and uploaded all of my files.

Now, I am staring at the PHP file that generates the output. It does not match up with what is output. In fact, I can comment out the whole thing and it doesn't change the fact that the old code is still being shown on the forum. I just don't understand why/how this is possible.

This code is in /library/xenforo/PHP/playerhandler.php
PHP:
<?php
class XenForo_PHP_playerhandler
{
    public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'forum_list_sidebar')
        {
            ob_start();
            require_once('players.php');
            $contents .= ob_get_contents();
            ob_end_clean();
        }
    }
}
?>

As you can see, it includes the "players.php" file, which is the file I am editing to change the output. Or, at least I thought it would change the output.

Now the Code Event has an Execute Callback of "XenForo_PHP_playerhandler :: includePhpFile". Ok, that matches up with the filepath that playerhandler.php resides in.

I just don't understand how the code in players.php is not what is being displayed on the site. It makes zero sense to me. Any ideas?
 
Are you sure you are editing the right file on the right server? You did just move servers so maybe something is confused there.
 
Yeah, I initially thought the same thing - maybe I just had the hosts mixed up. But, I am absolutely certain I am connecting to the correct host, and can't see how I'd be mixed up on the files either... I mean, I changed the \library\config.php file to go into debug mode, and that worked fine. I just navigated from there to get to my custom PHP files in \library\xenforo\php\
 
Just wanted to say a public thanks for the people who have posted in this thread, especially Arik for including this plugin so it's a case of just installing. I have successfully used this to include a banner PHP file in the top ad slot instead of using an iframe. Also, I have been able to get a banner insertion into a custom slot made easier/quicker by adding my own hook name to a template rather than adding iframe code at that point :)
 
is there a way to set the plugin/block above to show first on the sidebar instead of last?
Sure. Change the hook name to any available hook. You can find hooks in templates defined as xen:hook. You can also make your own hook (it's a bit dirty to do it this way).

Do note that this method is pretty old. It is encouraged to use the new template modifications system over template hooks where possible. If you want to put it in a location where a template hook doesn't exist, try using the TM system. It's more powerful and less messy.
 
Top Bottom