Skip to content

Instantly share code, notes, and snippets.

@jstneti01
Created January 19, 2017 06:41
Show Gist options
  • Save jstneti01/c276d1586a7672a79e1cd62e66dd3fba to your computer and use it in GitHub Desktop.
Save jstneti01/c276d1586a7672a79e1cd62e66dd3fba to your computer and use it in GitHub Desktop.
WordPress User Taxonomy
<?php
get_header();
$curauth = (get_query_var( 'author_name' )) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
$userid = $curauth->ID;
$org_terms = wp_get_object_terms( $userid, 'user_cat' );
$position = '';
if ( ! empty( $org_terms ) ) {
if ( ! is_wp_error( $org_terms ) ) {
foreach ( $org_terms as $org_term ) {
$position .= $org_term->name . ' ';
}
}
}
?>
<div>
<div>
<div>
<?php echo get_avatar( $userid, 512 ); ?>
<div>
<h6><?php echo $curauth->display_name; ?></h6>
<div><?php echo $position; ?></div>
</div>
</div><!-- .pb-layout-column-left -->
</div><!-- .pb-layout-column-left -->
<div class="pb-layout-column-right" style="visibility: visible;">
<div class="pb-team-text-box">
<h3 class="pb-team-text-box-member-name"><?php echo $curauth->display_name; ?></h3>
<div class="pb-team-text-box-member-position"><?php echo $position; ?></div>
<h6 class="pb-header">User Info:</h6>
<ul>
<?php if ( get_user_meta( $userid, 'meri_bday', true ) !== '' ) : ?>
<li>
<div>Age:</div>
<div><?php echo date("Y") - get_user_meta( $userid, 'meri_bday', true ); ?></div>
</li>
<?php endif;
if ( get_user_meta( $userid, 'meri_lang', true ) !== '' ) : ?>
<li>
<div>Languages:</div>
<div><?php echo get_user_meta( $userid, 'meri_lang', true ); ?></div>
</li>
<?php endif;
if ( get_user_meta( $userid, 'meri_edu', true ) !== '' ) : ?>
<li>
<div>Education:</div>
<div><?php echo get_user_meta( $userid, 'meri_edu', true ) ?></div>
</li>
<?php endif; ?>
</ul>
</div><!-- .pb-vertical-grid -->
<?php if ( get_user_meta( $userid, 'meri_know', true ) !== '' ) : ?>
<h6>Other knowledge:</h6>
<p><?php echo get_user_meta( $userid, 'meri_know', true ); ?></p>
<?php endif;
if ( get_user_meta( $userid, 'meri_ref', true ) !== '' ) : ?>
<h6>References:</h6>
<p><?php echo get_user_meta( $userid, 'meri_ref', true ); ?></p>
<?php endif;
if ( get_user_meta( $userid, 'meri_value', true ) !== '' ) : ?>
<h6>Added value:</h6>
<p><?php echo get_user_meta( $userid, 'meri_value', true ); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer();
<?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();
<?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