1. Customize
  2. Leaderboards
  3. Add a custom column to the leaderboard table

Add a custom column to the leaderboard table

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 );
Was this article helpful to you? Yes No

How can we help?