XF 2.1 How do I embed a PHP script in a Xenforo page?

Joe Blow

Active member
I'm just about to upgrade to XF2.1 and I need to embed a PHP script in a Xenforo page.

I can't find any instructions or tutorial on how to do this.

Can anyone tell me how it is done or point me in the direction of information that tells me what to do?

Thanks for any help.
 
  • Like
Reactions: arn

arn

Well-known member
I muddled my way through this before. I'm not the best person to answer. But hope this helps.


Create an AddOn. Answer "No" to Does your add-on need a Setup file. Name it DIRECTORY/NAME. In my case, it's AHK/BrowseImages

I created a PHP file called "Browse.php" in that directory which lives at src/addons/AHK/BrowseImages

Code:
<?php
namespace AHK\BrowseImages;
class Browse
{
  public static function getHtml()
  {
    $output=file_get_contents("file.txt");
    return $output;
  }

}

On the Page in the TEMPLATE Html, I added this:

Code:
<xf:callback class="\AHK\BrowseImages\Browse" method="getHtml"></xf:callback>

and it read the file and outputted it onto a XF page.

It was by trial/error based on posts here. So it may be wrong in ways.

arn
 

Joe Blow

Active member
Please explain what "embed a PHP script" does mean (to you).
Sorry for the delay in replying but I didn't get a notification that there was a reply in this thread.

I have a small PHP script that outputs a basic table with data retrieved from a simple database.

I want this script to display this output in a Xenforo page.

I have it working on XF 1.5 using the Kotomi Generic Script Bridge but there doesn't appear to be anything like that for XF 2.1
 

Joe Blow

Active member
I muddled my way through this before. I'm not the best person to answer. But hope this helps.


Create an AddOn. Answer "No" to Does your add-on need a Setup file. Name it DIRECTORY/NAME. In my case, it's AHK/BrowseImages

I created a PHP file called "Browse.php" in that directory which lives at src/addons/AHK/BrowseImages

Code:
<?php
namespace AHK\BrowseImages;
class Browse
{
  public static function getHtml()
  {
    $output=file_get_contents("file.txt");
    return $output;
  }

}

On the Page in the TEMPLATE Html, I added this:

Code:
<xf:callback class="\AHK\BrowseImages\Browse" method="getHtml"></xf:callback>

and it read the file and outputted it onto a XF page.

It was by trial/error based on posts here. So it may be wrong in ways.

arn

Thanks, I'll give this a try on the weekend and see what happens.
 
  • Like
Reactions: arn

Joe Blow

Active member
I muddled my way through this before. I'm not the best person to answer. But hope this helps.


Create an AddOn. Answer "No" to Does your add-on need a Setup file. Name it DIRECTORY/NAME. In my case, it's AHK/BrowseImages

I created a PHP file called "Browse.php" in that directory which lives at src/addons/AHK/BrowseImages

Code:
<?php
namespace AHK\BrowseImages;
class Browse
{
  public static function getHtml()
  {
    $output=file_get_contents("file.txt");
    return $output;
  }

}

On the Page in the TEMPLATE Html, I added this:

Code:
<xf:callback class="\AHK\BrowseImages\Browse" method="getHtml"></xf:callback>

and it read the file and outputted it onto a XF page.

It was by trial/error based on posts here. So it may be wrong in ways.

arn

Hi, I tried this but it didn't quite work as expected. I see that your file is named file.txt. Is that because you're outputting a text file?

I'm trying to output a php file but unfortunately it just dumped the content of the file onto the page like this:

screenshot-www.4mok.com-2020.07.25-14_47_00.png

I feel like I'm getting close but something still isn't right. Wish I knew how to fix.
 

arn

Well-known member
That was just an example of a small amount of php.

Replace “ $output=file_get_contents("file.txt");” with your PHP.

it might as well have been

Code:
echo “hello world”;
 
Top