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

Can you add a variable to $visitor object?

Is there a way to add a variable to the $visitor object? We are linking an external website account with a XenForo account and I'd like to store the API tokens in the $visitor object.

Chris D

XenForo developer
Staff member
You could also store them temporarily in the user's session or permanently in the database. There's also a framework for storing and accessing external accounts data. We mostly use it for storing external authentication keys and associated data.
Thanks for the help guys. Can I just add something like the code below to my code event listener?

$visitor["api_token"] = "the token value";

Will the "api_token" value be available anytime you have the $visitor object?
I'm using the visitor_setup event listener.

In the visitor_setup event listener function I have:

class Passport_Listener_Listener
public static function visitor_setup(XenForo_Visitor &$visitor)
$visitor["api_token"] = "abc123";

On our wordpress side of the site I have:
$visitor = XenForo_Visitor::getInstance()->toArray();

I log into XenForo and browse to wordpress side of our site and the var_dump doesn't have the "api_token" or it's value. So I'm missing something or it's not getting saved. A var_dump in the XenForo function shows it (of course).

Chris D

XenForo developer
Staff member
If that code is being used on the WordPress side, I would guess that whatever is talking to XF from that side isn't initialising the complete set of XF dependencies, some of which would be initialising the various code event listeners.
Thank you Chris D. Once I looked over the wordpress side I did see they didn't load the dependencies. Thanks for all the help, really appreciate it.