Custom Leaderboard Types
Custom Leaderboard Types
Since leaderboards can be based on so many different things, the plugin was made as developer friendly as possible, in order to allow customizations. In this guide, I will walk you through how a custom leaderboard type that will allow us to generate a leaderboard for our current year.
Add Our Custom Type
First, we will need to add our type the list of leaderboard types that are shown in the admin area. This list is also used to tell the plugin that a custom type might exists. We will do this using the mycred_bp_leaderboard_types filter. You can use this filter not just to add your own type but also to rename existing ones or for example removing unwanted types.
The Leaderboard Query
Next we need to handle the database query and setup of our leaderboard type. This is by far the most important function. To do this, we can use the mycred_bp_leaderboard_get filter. Our custom function needs to check if a cached leaderboard exists, if not query a new one and add support for appending users to the end of the leaderboard (if enabled).
The Position Query
Finally we need to handle a users leaderboard position. For this, we will be using the mycred_bp_leaderboard_find_position filter. This is mainly used when a user is appended to a leaderboard (assuming it is something you use). If this is not done, all appended users position will be the total number of group members.
It is important to remember that unlike the mycred_bp_leaderboard_get filter, this filter only runs when a users cached position has been deleted and a new query is needed.