XF 2.2 Members and visitors in wordpress homepage

on my home page made with wordpress I would like to display the number of total members and online members/visitors.
How can I do this?
Thanks in advance to those who would like to answer
 
Simply inject this code to your wp plugin:

PHP:
$dir = '/var/www/html/xf';
require($dir . '/src/XF.php');
\XF::start($dir);

$visitor = \XF::visitor();

\XF::dump(\XF::app()->forumStatistics);

Output will be array
["threads"]=>
string(2) "20"
["messages"]=>
string(2) "49"
["users"]=>
int(16)
["latestUser"]=> array:55

Reference
 
Will this work if your XF forum is hosted in a subdomain? My WP site is on domain.com etc and my forum is on a subdomain of that.
 
The proposed solution above works, as long as you can access the XF files from the WP installation (i.e. they use the same filesystem. The domains don't matter).

The caveat of this method is that you're loading XF every time you're loading WP, which is a serious overkill (page load performance) especially if your end goal is just a few stats.

The proper way to do what you ask for is to use the XF API (here is the relevant endpoint), and to load the results in your WP plugin with AJAX. You can use this WP plugin as a reference. Or at the very least use the solution above but with AJAX.
 
Last edited:
Back
Top Bottom