• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Customization with PHP Code

#1
Hello,

I'm trying to figure out a way to grab content using file_get_contents and display it on the thread page under each username.

There a way to do this within the template code? or can I pull a php file some how?

Didn't find anything on the help page, Assistance would be much appreciated.

Regards,
Cory
 

Jake Bunce

XenForo moderator
Staff member
#2
That requires an event listener with a template hook. Here is a code example using the body hook:

http://xenforo.com/community/threads/how-to-create-a-bridge.28515/#post-331631

In your case you can use one of the hooks from the message_user_info template, such as message_user_info_text.

This will allow you to execute your own PHP code to get the file, and then append the output to the contents of that hook. This obviously requires coding ability.
 
#3
Hello,

I've enabled Development Tools and I've came across some issues, maybe you can point out what I'm doing wrong.

Created 'New Add-On' under name 'MCBans' id/title (No:
Installation Code: or
Uninstallation Code: defined)

Created 'Code Event Listener':

Listen to Event: template_hook
Execute Callback: MCBans :: Lookup
Checked:
Enable Callback Execution
Description: MCBans Lookup
Add-On: MCBans

Created Folder: MCBans in library/
Created Folder: Listener in library/MCBans

Created File: MCBans.php in library/MCBans/

Code:
Code:
<?php
 
class MCBans
{
public static function Lookup($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'message_user_info')
{
$url = "http://justonemoreblock.com/mcbans/?username=";
$contents = file_get_contents($url);
echo $contents;
}
}
}
 
?>
No clue if this would work or not.

Basically this will connect to a remote site, the username name is input by a $custom.field, so I won't have to define, by ideal way is too include it the template like

{MCBans.Lookup}{$custom.userfield.minecraft_username} and it will pull the username from the already added database.

http://justonemoreblock.com/mcbans/?username=rogerthemaestro (Random account pulled off MCBans)

This info will be displayed. (Template will be re-designed once this works.

Please any assistance will be appreciated.

Regards,
Cory
 

Jake Bunce

XenForo moderator
Staff member
#4
You are having naming problems. For example, use this callback:

MCBans_Listener :: Lookup

Then your file would be located at:

library/MCBans/Listener.php

And the class name in the file would be:

MCBans_Listener

And this code is wrong:

Code:
$url = "http://justonemoreblock.com/mcbans/?username=";
$contents = file_get_contents($url);
echo $contents;
You should append to the value of $contents, not overwrite it. And don't echo or return anything. You just modify $contents and that's it.
 
#7
I think I'm finally getting somewhere...

Example Link:
http://www.bebopvox.com/forum/threads/where-will-towny-go.9219/#post-22245

Now if you look at the bottom of the post in the names, it shows the data, but it's not pulling the data and think it's because of my variable.

PHP:
<?php
 
class MCBans_Listener
{
    public static function Lookup($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'message_user_info_text')
        {
                    $url = "http://justonemoreblock.com/mcbans/?username=$hookParams";
            $contents = file_get_contents($url);
        }
    }
}
 
?>
Thinking $hookParams is wrong.

This is my template code:
Code:
<xen:hook name="message_user_info_text" params="{xen:array 'minecraft_username={$user.customFields.minecraft_username}'}"></xen:hook>
The {$user.customFields.minecraft_username} comes from a User Custom Field a user enters upon signup.

Notice the file contents is slow, so might add the actual api in the function instead if I get the file_get_contents working.
 

Jake Bunce

XenForo moderator
Staff member
#8
I don't recommend modifying the template. Change it back.

"user" is one of the default params for that hook, so you can access it with:

Code:
$hookParams['user']
From there you can index into the custom fields:

Code:
$hookParams['user']['customFields']['minecraft_username']
That should work. You may have to debug it to make sure you are getting the right value.
 
#9
After tinkering it with a bit, I did finally get it to work.

Listener:
PHP:
<?php
 
class MCBans_Listener
{
    public static function Lookup($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'message_user_info_text')
        {
                    $url = "http://justonemoreblock.com/mcbans/?username=".$hookParams['user']['customFields']['minecraft_username']."";
            $contents = file_get_contents($url);
                       
        }
    }
}
 
?>
Template Code:
Code:
<xen:hook name="message_user_info_text" params="{xen:array 'user={$user}'}"></xen:hook>
I would like to change its location instead of the bottom of the avatar (loads really slow as it's trying each username.) I would like to place it when you click on a username from the forum shows all the information of the user.

Picture for the example...

What would that post template be?
 

Attachments

Jake Bunce

XenForo moderator
Staff member
#10
This is the template for the member card:

Admin CP -> Appearance -> Templates -> member_card

There are two hooks:

member_card_links
member_card_stats


But neither hook passes the user record. You will need to modify the template to pass that with the hook.
 
#11
OK, I didn't have any of these templates because I'm using a different theme.

member_card_links
member_card_stats

I do however; Have member_card template and apparent it does pass the user hook, so I implemented my code and worked successfully, just need to make it look pretty now.

The only thing I'm having an issue is, somehow it's still passing in the side of the template and I don't have any template hook in regards to that other than the member_card.

Nevermind about that as I found a "message_user_info_text" in a template file as a hook name which would be required.

I modified by hook name to be unquie: mcbans_lookup

and it works! and no more side information.

PHP:
<?php
 
class MCBans_Listener
{
    public static function Lookup($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'mcbans_lookup')
        {
                    $url = "http://justonemoreblock.com/mcbans/?username=".$hookParams['user']['customFields']['minecraft_username']."";
            $contents = file_get_contents($url);
                     
        }
    }
}
 
?>
I'd like to take this time to thank you Jake for all your help and I've successfully got this to work. :)

As for anyone who wants to use this code, I don't mind - I may create a tutortial with Jake as credits in helping me in accomplishing this...One last thing that isn't required is to make it not work off my API for MCbans. :) (Removing file get contents)

Preview:
http://bebopvox.com/forum/threads/where-will-towny-go.9219/#post-22245

Btw is there some kind of if moderator code? I can make it show only mods+ can view it?

Regards,
Cory