Display Data Explorer query results in WordPress (with TwigAnything)

Display newly registered Discourse users in WordPress

This quick how-to is about displaying the list of the newly registered users from Discourse in WordPress.

I implemented this on our WordPress website, which is complementary to our Discourse instance, so sharing a quick how-to with illustrations while it’s fresh in my memory.

Here is how it looks on our WordPress website:

It lists the latest 8 registered users who reached Trust Level 1.

If your forum is very active and you have new user registrations every single day, we can elaborate the guide to show two lists: “Today registrations” and “Yesterday registrations”; let me know if this is something you’d like to try.

You can also easily adapt the steps below to show how many days (hours) ago the user registered, or the exact registration date, or additionally show their names and/or their avatars.

I’m not showing the avatars because it’s rare that new users upload their avatars immediately, so there will be a lot of placeholders, i.e. a circle of a particular color with a capital letter in it.

Steps

Step 1. Create a new query in your Data Explorer in Discourse:

SELECT id as user_id,
  username, username_lower,
  name, title, created_at,
  uploaded_avatar_id,
  suspended_at, suspended_till
FROM users
WHERE admin = false
  AND moderator = false
  AND trust_level >= 1
  AND active = true
  AND staged = false
  AND silenced_till IS NULL
  AND suspended_till IS NULL

ORDER BY created_at DESC
LIMIT 8

You should tune the query to your needs:

  • LIMIT 8 — how many users you would like to show in the list
  • AND trust_level >= 1 — what is the minimum trust level; in this example, users with trust level 1 and above will be shown, but not users with trust level 0; users hold level 0 after they sign up and before they do any activity;

Once you save the query, grab its unique ID from the URL:

Step 2. Create a new Twig Template in WordPress:

image

Configure the template:

The template code:

{% for row in data.rows %}
  <a href="https://forum.kozovod.com/u/{{ row[1] }}">{{ row[1] }}</a>
  {% if not loop.last %},{% endif %}
{% endfor %}

Source Type: URL
Data Format: JSON-encoded string

Cache lifetime in seconds: 300
You can set it to whatever is suitable.
The Discourse API will be queries only one time per 300 seconds (5 minutes).
I do not recommend to leave it empty.

HTTP Request Method: POST

Data URL

https://forum.kozovod.com/admin/plugins/explorer/queries/35/run.json?api_username=...&api_key=...

Change https://forum.kozovod.com to your website.
Change 35 to the Data Explorer Query ID we grabbed in the previous step.
Insert your Discourse api_username and api_key

Now click on “Save” and then on “Preview changes”, make sure the user list is fetched and is displayed:

Display in WordPress

Now, just copy the shortcode and embed it in a widget in your sidebar:

A nice box was provided by the theme for me, so I just had to insert the shortcode:

image

4 Likes