I need to create a page for team coach's to view player data from a custom post type. Players are assigned to a "team" using a custom taxonomy called "team"
I have checkboxes setup for custom user meta which is dynamically populated by a custom taxonomy called "teams".
I need to display all checked options from the user's profile on a page with links to their corresponding taxonomy pages.
Ex.
[X] Team A
[X] Team B
[ ] Team C
[X] Team D
On the page it will then display the team Names (Ex. Team A) and permalink to each team if they are checked in the user's profile.
How can this be achieved?
Function to create and save Custom User Meta Fields from Taxonomy
function coach_team_access( $user ) {
$teams = get_terms('team', array('hide_empty' => false));
$user_teams = get_the_author_meta( 'user_teams', $user->ID );
?>
<table class="form-table">
<tr>
<th>Team Access:</th>
<td>
<?php
if ( count( $teams ) ) {
foreach( $teams as $team ) { ?>
<p><label for="user_iteams_<?php echo esc_attr( $team->slug); ?>">
<input
id="user_teams_<?php echo esc_attr( $team->slug); ?>"
name="user_teams[<?php echo esc_attr( $team->term_id ); ?>]"
type="checkbox"
value="<?php echo esc_attr( $team->term_id ); ?>"
<?php if ( in_array( $team->term_id, $user_teams ) ) echo ' checked="checked"'; ?> />
<?php echo esc_html($team->name); ?>
</label></p><?php
}
} ?>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'coach_team_access' );
add_action( 'edit_user_profile', 'coach_team_access' );
// store access
function coach_team_access_save( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'user_teams', $_POST['user_teams'] );
}
add_action( 'personal_options_update', 'coach_team_access_save' );
add_action( 'edit_user_profile_update', 'coach_team_access_save' );