How to show XenForo forum stats with with a shortcode

Table of Contents

  • Using the [xf] shortcode
  • Developer function

As of XFtoWP 1.5.5+ you can now easily display statistics about your forum with the xf shortcode and an accompanying PHP function.

Using the shortcode

For large statistics you can easily format them into the shorthand value with an accessible title tag for a full count.

For example:

The Kolakube support forums has 4.9K threads (that’s 4,912) with 27,973 posts and 1 user(s) online now.

The complete list of forum statistics you can post:

  • threads | Total number of threads
  • posts | Total number of messages
  • users | Total number of members
  • online | Number of users online now

Simply plug a forum stat above in to the xf shortcode like the below example, which shows the total number of threads.

[xf threads]

To format long numbers or just show the shorthand value, add the short parameter to the shortcode:

[xf threads short]

That will turn the total number of 4,912 into 4.9K. You can hover your mouse over the shorthand value to see the total number in a title tag.

Developer function

Under the hood, the xf shortcode uses the xf_stat() PHP function to display the forum statistics from the database.

When working in templates you can also use this function to easily get formatted numbers like so:

<?php echo xf_stat( 'threads' ); ?>

To activate the shorthand display, make the second parameter true:

<?php echo xf_stat( 'threads', true ); ?>

If, for any reason, you need to render the shortcode directly in your templates you can use the following:

 <?php echo do_shortcode( '[xf threads short'] ); ?>