1. Customize
  2. Leaderboards
  3. Custom leaderboard columns

Custom leaderboard columns

You are able to add, remove and rename columns show on the Leaderboards add-on.

The columns displayed are:

  • display_name: Name
  • avatar: Avatar

Also, there are columns generated by metrics selected, those columns are composed by the type’s slug and the type label.

For example, if you have a points type named “Credits” with the slug “credits” on columns it will be displayed as:

  • credits: Credits

It’s important to know this since you can use the type’s slug to rename or remove them as you want on the following snippets.

Renaming and removing columns

function my_prefix_custom_leaderboard_columns( $columns, $leaderboard_id, $leaderboard ) {

    // Rename user column
    $columns['display_name'] = __( 'Member' );

    // Rename the "credits" points type column
    $columns['credits'] = __( 'My custom label for Credits' );

    // Remove the avatar column
    unset( $columns['avatar'] );

    return $columns;

}
add_filter( 'gamipress_leaderboards_leaderboard_columns_info', 'my_prefix_custom_leaderboard_columns', 10, 3 );

Adding a custom column

The following snippet shows how to register a custom column to a leaderboard and how to handle the output of this custom column.

If you want to render an user meta value and you don’t know exactly the name of this meta key, you can use a plugin like JSM’s Show User Meta to see all stored user meta keys.

function my_prefix_custom_leaderboard_column( $columns, $leaderboard_id, $leaderboard ) {

    // You can limit the custom column to just a specific leaderboard
    if( $leaderboard_id !== 123 )
        return $columns;

    // Add the new column
    // The key "my_custom_column" will be used to filter the column output in next function
    // The "Custom Column" will be the column title
    $columns['my_custom_column'] = __( 'Custom Column' );

    return $columns;

}
add_filter( 'gamipress_leaderboards_leaderboard_columns_info', 'my_prefix_custom_leaderboard_column', 10, 3 );

function my_prefix_custom_leaderboard_column_output( $output, $leaderboard_id, $position, $item, $column_name, $leaderboard_table ) {

    // Example of output an user meta
    return get_user_meta( $item['user_id'], '_custom_meta_key', true );

}
// This filter dynamically changes based on the column key following the next pattern: gamipress_leaderboards_leaderboard_column_{key}
// In previous function the new column key is "my_custom_column", so the filter will be: gamipress_leaderboards_leaderboard_column_my_custom_column
add_filter( 'gamipress_leaderboards_leaderboard_column_my_custom_column', 'my_prefix_custom_leaderboard_column_output', 10, 6 );

How can we help?