Created
January 19, 2017 06:41
-
-
Save jstneti01/c276d1586a7672a79e1cd62e66dd3fba to your computer and use it in GitHub Desktop.
WordPress User Taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
get_header(); | |
<div> | |
<ul> | |
<?php | |
$tax_name = get_queried_object()->name; | |
$number = 4; // Users per page. | |
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // Current pagination number. | |
if ( $paged == 1 ) { | |
$offset = 0; | |
} else { | |
$offset = ( $paged - 1 ) * $number; | |
} // Number of users passed (offset) in pages. From second page on. | |
// Get all user IDs that belong to this User Taxonomy we are looking at. | |
$args = array( | |
'orderby' => 'display_name', | |
'order' => 'ASC', | |
'role' => 'shop_staff', | |
'meta_key' => 'muser_cat', | |
'meta_value' => get_queried_object()->term_id, | |
'fields' => array( 'ID', 'user_login' ), | |
'offset' => $offset, | |
'number' => $number, | |
'paged' => $paged, | |
); | |
$user_q = new WP_User_Query( $args ); | |
$qresults = $user_q->results; // Results from WP Query. | |
if ( ! empty( $qresults ) ) : | |
foreach ( $qresults as $usr ) : | |
// Loop through each of them and output their profiles. | |
$usr_id = $usr->ID; | |
?> | |
<li> | |
<div> | |
<div class="pb-layout-column-left"> | |
<a href="<?php printf( '%s/profile/%s', site_url(), $usr->user_login ); ?>"> | |
<div> | |
<?php echo get_avatar( $usr_id, 512 ); ?> | |
<div> | |
<h6 class="pb-image-text-caption"><?php the_author_meta( 'first_name', $usr_id ); ?> <?php the_author_meta( 'last_name', $usr_id ); ?></h6> | |
<div class="pb-image-text-description"><?php echo $tax_name; ?></div> | |
</div> | |
</div> | |
</a> | |
</div><!-- .pb-layout-column-left --> | |
<div class="pb-layout-column-right"> | |
<div class="pb-team-text-box"> | |
<p><?php the_author_meta( 'description', $usr_id ); ?></p> | |
</div> | |
</div><!-- .pb-layout-column-right --> | |
</div> | |
</li> | |
<?php endforeach; | |
else : | |
echo '<p>No profiles in this category.</p>'; | |
endif; ?> | |
</ul> | |
</div> | |
<?php // Pagination. | |
$total_user = $user_q->total_users; | |
$total_pages = ceil( $total_user / $number ); // Total pages that should be created. | |
if ( $total_pages > 1 ) { | |
echo '<div class="woocommerce"><nav id="usr-pagination" class="woocommerce-pagination">'; | |
echo paginate_links( array( | |
'base' => get_pagenum_link(1) . '%_%', | |
'format' => 'page/%#%/', | |
'current' => $paged, | |
'total' => $total_pages, | |
'prev_text' => '←', | |
'next_text' => '→', | |
'end_size' => 1, | |
'mid_size' => 1, | |
'type' => 'list', | |
) ); | |
echo '</nav></div>'; | |
} | |
get_footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'register_user_taxonomy' ); | |
/** | |
* Register new User Taxonomy user_cat | |
*/ | |
function register_user_taxonomy() { | |
$labels = array( | |
'name' => 'User Category', | |
'singular_name' => 'User Category', | |
'search_items' => 'Search User Category', | |
'all_items' => 'All User Categories', | |
'parent_item' => 'Parent User Category', | |
'parent_item_colon' => 'Prent User Category', | |
'edit_item' => 'Edit User Category', | |
'update_item' => 'Update User Category', | |
'add_new_item' => 'Add New User Category', | |
'new_item_name' => 'User Category Name', | |
'menu_name' => 'User Category', | |
); | |
$args = array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'type' ), | |
); | |
register_taxonomy( 'user_cat' , 'user' , $args ); | |
} | |
add_action( 'admin_menu', 'add_user_cat_menu' ); | |
/** | |
* Add Admin Submenu | |
*/ | |
function add_user_cat_menu() { | |
add_submenu_page( 'users.php' , 'User Category', 'User Category' , 'add_users', 'edit-tags.php?taxonomy=user_cat' ); | |
} | |
add_action( 'show_user_profile', 'show_user_cat' ); | |
add_action( 'edit_user_profile', 'show_user_cat' ); | |
/** | |
* Show and Edit the category in User profile. | |
* | |
* @param Object $user User. | |
*/ | |
function show_user_cat( $user ) { | |
// Get the terms that the user is assigned to. | |
$assigned_terms = wp_get_object_terms( $user->ID, 'user_cat' ); | |
$assigned_term_ids = array(); | |
foreach ( $assigned_terms as $term ) { | |
$assigned_term_ids[] = $term->term_id; | |
} | |
// Get all the terms we have. | |
$user_cats = get_terms( 'user_cat', array( 'hide_empty' => false ) ); | |
?> | |
<table class="form-table"> | |
<tr> | |
<th><label>User Category</label></th> | |
<td> | |
<?php // List the terms as checkbox, make sure the assigned terms are checked. | |
foreach( $user_cats as $cat ) { ?> | |
<input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if ( in_array( $cat->term_id, $assigned_term_ids ) ) echo 'checked=checked';?> name="user_cat[]" value="<?php echo $cat->term_id;?>"/> | |
<?php | |
echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>'; | |
echo '<br />'; | |
} ?> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'personal_options_update', 'save_user_cat' ); | |
add_action( 'edit_user_profile_update', 'save_user_cat' ); | |
/** | |
* Update/Save on User update. | |
* | |
* @param Int $user_id User ID. | |
*/ | |
function save_user_cat( $user_id ) { | |
if ( current_user_can( 'edit_user', $user_id ) ) { | |
$user_terms = $_POST['user_cat']; | |
$terms = array_unique( array_map( 'intval', $user_terms ) ); | |
wp_set_object_terms( $user_id, $terms, 'user_cat', false ); | |
// Save the category to usermeta. We need this to filter and show users on taxonomy archive page. | |
update_usermeta( $user_id, 'muser_cat', $_POST['user_cat'][0] ); | |
// Make sure you clear the term cache. | |
clean_object_term_cache( $user_id, 'user_cat' ); | |
} | |
} | |
add_action( 'init', 'tc_author_base' ); | |
/** | |
* Remove author slug, replace with 'profile' | |
*/ | |
function tc_author_base() { | |
global $wp_rewrite; | |
$wp_rewrite->author_base = 'profile'; | |
} | |
add_action( 'pre_get_posts', 'user_cat_pagination' ); | |
/** | |
* Add user_cat taxonomy to pagination. | |
* | |
* @param Object $query Query. | |
*/ | |
function user_cat_pagination( $query ) { | |
if ( $query->is_tax( 'user_cat' ) ) { | |
$query->set( 'posts_per_page', 1 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment