Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active March 24, 2023 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kadimi/823419cad97dc0eeed9826075b028692 to your computer and use it in GitHub Desktop.
Save kadimi/823419cad97dc0eeed9826075b028692 to your computer and use it in GitHub Desktop.
For SportsPress - Add a player list on the fly to the current team.
<?php
/**
* Add a player list on the fly to the current team.
*/
add_action(
'init',
function () {
/**
* Configuration.
*
* @var array {
* @type boolean filter_by_league Filter by league.
* @type boolean filter_by_season Filter by season.
* @type boolean filter_by_current_team Filter by current team.
* @type int priority Filter priority. Use 9 or lower to place the additional content above
* any other SportsPress elements.
* }
*/
$config = array(
'filter_by_league' => true,
'filter_by_season' => true,
'filter_by_current_team' => true,
'priority' => 0,
);
/* That's all, stop editing! Happy sportspress'ing. */
add_filter(
'the_content',
function ( $content ) use ( $config ) {
$object = get_queried_object();
/**
* Exit if this is not a SportsPress team.
*/
if ( 'sp_team' !== $object->post_type ) {
return $content;
}
/**
* Build the output.
*
* We use the shortcode `[player_list team=? seasons=? leagues=?]`.
* If `filter_by_current_team` is false, we will filter players and only
* show the ones whose team is the current one.
*/
$id = $object->ID;
$league = $config['filter_by_league'] ? get_option( 'sportspress_league' ) : false;
$season = $config['filter_by_season'] ? get_option( 'sportspress_season' ) : false;
$force_current_teams_cb = $config['filter_by_current_team']
? function ( $args, $team ) {
$args['meta_query'][] = array(
array(
'key' => 'sp_current_team',
'value' => $team,
),
);
return $args;
}
: function ( $args ) {
return $args;
};
add_filter( 'sportspress_player_list_args', $force_current_teams_cb, 10, 2 );
$additional_content = do_shortcode(
sprintf(
'[player_list team=%d %s %s]',
$id,
$season ? "seasons=$season" : '',
$league ? "leagues=$league" : ''
)
);
remove_filter( 'sportspress_player_list_args', $force_current_teams_cb, 10, 2 );
/**
* Done
*/
return $content . $additional_content;
},
$config['priority']
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment