1. 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?

Discussion in 'XenForo Development Discussions' started by Randall, Aug 17, 2015.

  1. Randall

    Randall Member

    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.
  2. batpool52!

    batpool52! Well-Known Member

    visitor_setup code even listener maybe?
  3. Chris D

    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.
    SneakyDave likes this.
  4. Randall

    Randall Member

    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?
  5. Chris D

    Chris D XenForo Developer Staff Member

    Depends which event listener. If it's the visitor_setup event listener, yes, it should.
  6. Randall

    Randall Member

    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).
  7. Chris D

    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.
    Marcus likes this.
  8. Randall

    Randall Member

    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.
    Chris D likes this.

Share This Page