How to change the player information sections

Sports Bench has two locations where player information is displayed by default. They typically consist of the player’s photo, name and other information. But sometimes, that information isn’t quite what you need

Fortunately, with the use of filters, it’s possible to customize these sections without having to write a whole ton of code. So here’s how to customize the player information sections.

Dealing with filters

If you checked out the post on how to change the team information section, you’ll be happy to know that the process is basically the same. Otherwise, we’re going to be using custom filters to make our changes.

If you’ve never dealt with filters before, you should really check out the WordPress codex section on them before continuing on. But the gist of them is that filters can take in some sort of data and make changes to it before it’s outputted. And you can add as many filters to a hook as you want.

So with that out of the way, let’s get started in customizing the player information.

Player page information section

So the first section that we’ll change is the player information section on the player page. If you’re using the Sports Bench theme or the player page shortcode, this is the section at the top of the page, usually with the player’s photo, name and other information.

This section uses the sports_bench_player_information filter hook. Here’s the default filter for this section which you can use to help you customize the section.

function sports_bench_do_player_information( $html, $player, $team ) {
$html .= '<div class="large-4 medium-4 small-12 columns">';
if ( $player->get_player_photo() ) {
$html .= '<div class="player-photo">';
$html .= $player->get_player_photo();
$html .= '</div>';
} else {
$html .= '<div class="player-photo">';
$html .= '<img src="' . plugins_url( '../images/mystery-person.jpg', __FILE__ ) . '" alt="mystery-person" />';
$html .= '</div>';
}
$html .= '</div>';

$html .= '<div class="large-8 medium-8 small-12 columns">';
$html .= '<h2 class="player-name">' . $player->player_first_name . ' ' . $player->player_last_name . '</h2>';
$html .= '<h4 class="player-details">' . $player->player_position . '</h4>';
$html .= '<h4 class="player-details">' . $team->team_name . '</h4>';
if ( '' !== $player->player_home_city && '' !== $player->player_home_state ) {
$html .= '<h4 class="player-details">' . $player->player_home_city . ', ' . $player->player_home_state . '</h4>';
} elseif ( '' !== $player->player_home_city ) {
$html .= '<h4 class="player-details">' . $player->player_home_city . '</h4>';
} elseif ( '' !== $player->player_home_state ) {
$html .= '<h4 class="player-details">' . $player->player_home_state . '</h4>';
}
if ( '' !== $player->get_age() ) {
$html .= '<h4 class="player-details">' . __( 'Age: ', 'sports-bench' ) . $player->get_age() . '</h4>';
}
if ( '' !== $player->player_height && 0 < $player->player_weight ) {
$html .= '<h4 class="player-details">' . stripslashes( $player->player_height ) . ' | ' . $player->player_weight . '</h4>';
} elseif ( '' !== $player->player_height ) {
$html .= '<h4 class="player-details">' . stripslashes( $player->player_height ) . '</h4>';
} elseif ( 0 < $player->player_weight ) {
$html .= '<h4 class="player-details">' . $player->player_weight . '</h4>';
}
$html .= '<h4 class="player-seasons-played">' . __( 'Seasons Played: ', 'sports-bench' ) . $player->get_seasons_played() . '</h4>';
$html .= '<div id="sports-bench-player-id" class="hide">' . $player->player_id . '</div>';
$html .= '</div>';

return $html;
}
add_filter( 'sports_bench_player_information', 'sports_bench_do_player_information', 10, 3 );

You can remove this default filter so you can add your own by calling remove_action(‘sports_bench_player_information’, ‘sports_bench_do_player_information’);.

Player listing page information section

The other player information section is on the player listing page. This is the page that shows where you can select the player to view. This also has the player’s photo, name and other items.

To change this, you’ll need to use the sports_bench_player_listing_information filter hook. Here’s the default usage to help you customize the player listing information section on your site.

function sports_bench_do_player_listing_information( $html, $player, $team, $num_players, $count ) {
if ( $count % 2 == 0 ) {
$html .= '<div class="row sports-bench-row">';
}
$html .= apply_filters( 'sports_bench_before_player_info', '', $team );
$html .= '<div class="large-6 medium-6 small-12 columns">';
$html .= '<a href="' . $player[ 'player_page' ] . '">';
$html .= '<aside class="player-info clearfix" style="border-top: 2px solid ' . $team->team_primary_color .'">';
$html .= '<h4 class="playername">' . $player[ 'first_name' ] . ' ' . $player[ 'last_name' ] . '</h4>';
$html .= '<div class="right">';
$html .= $player[ 'photo' ];
$html .= '</div>';
$html .= '<p>';
$html .= $player[ 'position' ] . '<br />';
if ( '' !== $player[ 'age' ] ) {
$html .= $player['age'] . '<br />';
}
if ( '' !== $player[ 'home_city' ] && '' !== $player[ 'home_state' ] ) {
$html .= '<h4 class="player-details">' . $player[ 'home_city' ] . ', ' . $player[ 'home_state' ] . '</h4>';
} elseif ( '' !== $player[ 'home_city' ] ) {
$html .= '<h4 class="player-details">' . $player[ 'home_city' ] . '</h4>';
} elseif ( '' !== $player[ 'home_state' ] ) {
$html .= '<h4 class="player-details">' . $player[ 'home_state' ] . '</h4>';
}
$html .= '</p>';
$html .= '</aside>';
$html .= '</a>';
$html .= '</div>';
$html .= apply_filters( 'sports_bench_after_player_info', '', $team );
if ( ( $count % 2 == 1 ) or ( $count == $num_players ) ) {
$html .= '</div>';
}

return $html;
}
add_filter( 'sports_bench_player_listing_information', 'sports_bench_do_player_listing_information', 10, 5 );

So there you have it. If you’re needing to change the player informations sections, this is how to do it. You can also check out the Codex and How To’s pages for more information on how to customize and extend Sports Bench for your site.

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.