Customizing Player Statistics in SportsPress Using Hooks and Filters
If you’re building a sports website with SportsPress and need more than the default statistics, this guide shows you how to create and display a custom player statistic—Points per Match—using WordPress hooks and filters. Ideal for developers looking to extend SportsPress functionality.
What You’ll Build
A new calculated player stat:
Points per Match = Total Points ÷ Matches Played
This stat will be automatically calculated, included in player tables, and optionally styled to highlight high performers.
Step 1: Register the Custom Statistic
Add this code to your theme’s functions.php
file or your custom plugin:
function register_custom_stat_ppm($stats) {
$stats['ppm'] = array(
'name' => __('Points per Match', 'sportspress'),
'column' => true, // Display in tables
'calculate'=> true, // Enable auto-calculation
);
return $stats;
}
add_filter('sportspress_player_stats', 'register_custom_stat_ppm');
Step 2: Define the Calculation Logic
Use a filter to define how the stat is calculated:
function calculate_stat_ppm($equation, $stat, $player_id) {
$points = get_post_meta($player_id, 'sp_points', true);
$matches = get_post_meta($player_id, 'sp_matches', true);
return ($matches > 0) ? round($points / $matches, 2) : 0;
}
add_filter('sportspress_stat_ppm_equation', 'calculate_stat_ppm', 10, 3);
Make sure that 'sp_points'
and 'sp_matches'
match the actual stat slugs used in your SportsPress configuration.
Step 3: Add the Stat to Player Tables
To display this new stat in frontend tables:
- Navigate to SportsPress > Configure > Player Lists
- Edit the layout and add the column with key
ppm
Optional: Highlight Top Performers with Conditional Formatting
If you’d like to visually emphasize certain values, you can add custom styling:
function style_custom_stat_ppm($output, $value, $stat, $post_id) {
if ($stat === 'ppm' && $value > 2.0) {
return '<span style="color: green; font-weight: bold;">' . $value . '</span>';
}
return $value;
}
add_filter('sportspress_output_stat', 'style_custom_stat_ppm', 10, 4);
Conclusion
By using WordPress hooks and filters, you can extend SportsPress far beyond its default settings. Whether you’re building for a local league or a national club, this approach gives you full control over how stats are calculated and displayed.
Leave a Reply