Custom PHP in Pages/Threads

James W.

Member
Hey all!

I use XF for a smaller forum I run, but am looking to move my primary community from vBulletin 4.x to XenForo. I already have a test forum up and have transferred what I can. The current process involves trying to ensure that we can have the least functionality lost during the transfer. As such, I have spent quite some time searching for AddOns that will give us the same functionality many of our plugins do.

One I am not sure of that is essential to migration is the ability to make custom PHP pages or include custom PHP code that would be executed. We have several scripts that I have designed that are custom to us that run via a plugin which allows us to create custom PHP pages (though we also have the vB CMS plugin and can do the same there).

I have looked at SimplePortal and XenPorta2 to determine if they have this functionality, but have been unable to determine, and without knowing I don't plan to purchase something. So, is there an easy way to do this (include custom PHP) which would include database queries to grab information, etc.? Any help would be appreciated. We'd like to move to XF, but we need to ensure the core functionality we have developed can be migrated in some fashion.

Side note: If you'd like to help with our migration or consult (free of charge) shoot me a PM. ^^
 
There are several ways to create a new page. The easiest for a newcomer - even though definitly not the most professional one - would be to do the following:
  • Go to your ACP -> Applications -> Node Tree -> Create New Page
  • Uncheck 'Display in the node list' to ensure the page is not shown in your forum list
  • Set the url-portion, title and if needed description
  • Go to PHP-Callback
In PHP-Callback you see two fields: Class and Method. You'll now need to create a folder in your library-folder, we'll name him "Example" for this example. Inside your folder, create another folder called "ControllerPublic" and inside this folder a php-file called "Pages". So your structure at this point would be:
  • Example
    • ControllerPublic
      • Pages.php
You could name all of those the way you want, but I recommend keeping "ControllerPublic" fixed.

Your basic setup for the php-file would then be:
PHP:
<?php

public class Example_ControllerPublic_Pages extends XenForo_ControllerPublic_Abstract {

}

The class name derives from the directory structure, so change it accordingly to your folder names. The "Class" in the "Create New Page -> PHP Callback" is now the class you've created here, just enter it for now. You can now create methods inside that class and enter them in the field "method" to call them for your page. Your method has in any case to return the following:
PHP:
            $this->responseView(
                'Example_ViewPublic_Pages_METHOD',
                'example_template_name',
                $viewParams
            )

What you need to change here is the "Example_ViewPublic_Pages_METHOD" in the same way that you changed the class name in the beginning. Keep "ViewPublic". "Example_template_name" will become important a bit later, so change it to something unique that you remember. $viewParams is a php-array with references that you can store your data in.

After creating your method, you can save the php file and your page. When you open the page up now, the only thing you'll see will be a blank page.

In your ACP go to 'Appearance -> Templates' and create a new template. Make sure that you do this in the Master Style (there's a dropdown on the top left above the template list, before you hit "create new template". Give it the name chosen above and then just do your HTML-Markup here. The XenForo Template Syntax allows you to process your $viewParams inside that template.

That'd pretty much be the basics, allthough there's a lot more you could do to make it cleaner, better to handle and stuff, but it'll be easily done later with the work you've done by following the steps above once you've gained the required knowledge.
 
I see, very much like the vBulletin template syntax, which I attempted to avoid like the plague. I much prefer to simply use PHP and just display the code I want, though if there is not a nice way to do so or a nice plugin to allow it I suppose I will have to give in to the template syntax nonsense and learn it. I am assuming ALL of the data I would want to display would have to be passed through the $viewParams or something similar in order to be passed for use in the templates?
 
Can you accomplish what you want by an "include" of the PHP file you want, then edit that file as you wish with a text editor?

That's a question for both @James W. and @katsulynx, as well as @Brogan.


FYI, James, there is a Xenforo to WordPress bridge that is awesome for moving content to a blog style page. It's not perfect for my needs, but I need more than that. However, I'm playing around with it, as well as looking at just creating a landing page on my Xenforo forum.

If you were just looking for a landing page, check out the conditionals page @Brogan compiled. It's great for moving content. I haven't worked with it yet, but I did a lot of similar type coding on SMF forums.
 
Back
Top Bottom