Pass logged-in wordpress user to phpfreechat

I need to pass the display name for the currently logged-in WordPress 3.2.4 user to PhpFreeChat 2.0.4. The route for achieving this seems to be by using the PhpFreeChat hook functionality to provide the current username for the WordPress user.

In my PhpFreeChat config.local.php I have:

Read More
<?php
    $GLOBALS['pfc_hooks']['pfc.before.auth'][5] = function ($app, $req, $res) {
    return function ($hr) use ($app, $req, $res) {
        $username = "";
        foreach($_COOKIE as $key => $value) {
            if(preg_match('@^wordpress_logged_in_@', $key)) {
                $cookie = explode('|', $_COOKIE[$key]);
                $username = $cookie[0];
            }
        }
        $hr->login = $username;
       };
};

From what I have read, this should pass the username of the currently logged-in WordPress user to PhpFreeChat when it is launched.

This does not appear to work as no username is shown in the chat.

If I create a .php page with just the cookie-parsing code, it does give me a value.

Can anyone advise as to any other approach? Is there a way to pass the username to PhpFreeChat from within the WordPress template instead?

Related posts

Leave a Reply

2 comments

  1. Since phpfreechat-2.0.5, the pfc.before.auth hook has been refactored so you’ll have to adapt your code like that (notice the return $username at the end):

    <?php
      $GLOBALS['pfc_hooks']['pfc.before.auth'][5] = function ($app, $req, $res) {
        return function () use ($app, $req, $res) {
          $username = '';
          foreach ($_COOKIE as $key => $value) {
            if (preg_match('@^wordpress_logged_in_@', $key)) {
                $cookie = explode('|', $_COOKIE[$key]);
                $username = $cookie[0];
            }
          }
          return $username;
        };
      };
    
  2. The code was actually just missing a closing bracket ).

    The code itself works fine. I’ll leave the question here as it may prove useful to someone else.