Skip to content

Instantly share code, notes, and snippets.

@emanuelpoletto
Last active December 20, 2015 06:19
Show Gist options
  • Select an option

  • Save emanuelpoletto/6085090 to your computer and use it in GitHub Desktop.

Select an option

Save emanuelpoletto/6085090 to your computer and use it in GitHub Desktop.
/**
* Registers the 'profession' taxonomy for users. This is a taxonomy for the 'user' object type rather than a
* post being the object type.
*/
function my_register_user_taxonomy() {
register_taxonomy(
'profession',
'user',
array(
'public' => true,
'labels' => array(
'name' => __( 'Professions' ),
'singular_name' => __( 'Profession' ),
'menu_name' => __( 'Professions' ),
'search_items' => __( 'Search Professions' ),
'popular_items' => __( 'Popular Professions' ),
'all_items' => __( 'All Professions' ),
'edit_item' => __( 'Edit Profession' ),
'update_item' => __( 'Update Profession' ),
'add_new_item' => __( 'Add New Profession' ),
'new_item_name' => __( 'New Profession Name' ),
'separate_items_with_commas' => __( 'Separate professions with commas' ),
'add_or_remove_items' => __( 'Add or remove professions' ),
'choose_from_most_used' => __( 'Choose from the most popular professions' ),
),
'rewrite' => array(
'with_front' => true,
'slug' => 'author/profession' // Use 'author' (default WP user slug).
),
'capabilities' => array(
'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
'edit_terms' => 'edit_users',
'delete_terms' => 'edit_users',
'assign_terms' => 'read',
),
'update_count_callback' => 'my_update_profession_count' // Use a custom function to update the count.
)
);
}
/**
* Function for updating the 'profession' taxonomy count. What this does is update the count of a specific term
* by the number of users that have been given the term. We're not doing any checks for users specifically here.
* We're just updating the count with no specifics for simplicity.
*
* See the _update_post_term_count() function in WordPress for more info.
*
* @param array $terms List of Term taxonomy IDs
* @param object $taxonomy Current taxonomy object of terms
*/
function my_update_profession_count( $terms, $taxonomy ) {
global $wpdb;
foreach ( (array) $terms as $term ) {
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
do_action( 'edit_term_taxonomy', $term, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
do_action( 'edited_term_taxonomy', $term, $taxonomy );
}
}
/**
* http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress#comment-568682
*/
add_filter( 'parent_file', 'fix_user_tax_page' );
function fix_user_tax_page( $parent_file = '' ) {
global $pagenow;
if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'profession' && $pagenow == 'edit-tags.php' ) {
$parent_file = 'users.php';
}
return $parent_file;
}
/* Adds the taxonomy page in the admin. */
add_action( 'admin_menu', 'my_add_profession_admin_page' );
/**
* Creates the admin page for the 'profession' taxonomy under the 'Users' menu. It works the same as any
* other taxonomy page in the admin. However, this is kind of hacky and is meant as a quick solution. When
* clicking on the menu item in the admin, WordPress' menu system thinks you're viewing something under 'Posts'
* instead of 'Users'. We really need WP core support for this.
*/
function my_add_profession_admin_page() {
$tax = get_taxonomy( 'profession' );
add_users_page(
esc_attr( $tax->labels->menu_name ),
esc_attr( $tax->labels->menu_name ),
$tax->cap->manage_terms,
'edit-tags.php?taxonomy=' . $tax->name
);
}
/* Create custom columns for the manage profession page. */
add_filter( 'manage_edit-profession_columns', 'my_manage_profession_user_column' );
/**
* Unsets the 'posts' column and adds a 'users' column on the manage profession admin page.
*
* @param array $columns An array of columns to be shown in the manage terms table.
*/
function my_manage_profession_user_column( $columns ) {
unset( $columns['posts'] );
$columns['users'] = __( 'Users' );
return $columns;
}
/* Customize the output of the custom column on the manage professions page. */
add_action( 'manage_profession_custom_column', 'my_manage_profession_column', 10, 3 );
/**
* Displays content for custom columns on the manage professions page in the admin.
*
* @param string $display WP just passes an empty string here.
* @param string $column The name of the custom column.
* @param int $term_id The ID of the term being displayed in the table.
*/
function my_manage_profession_column( $display, $column, $term_id ) {
if ( 'users' === $column ) {
$term = get_term( $term_id, 'profession' );
echo $term->count;
}
}
/* Add section to the edit user page in the admin to select profession. */
add_action( 'show_user_profile', 'my_edit_user_profession_section' );
add_action( 'edit_user_profile', 'my_edit_user_profession_section' );
/**
* Adds an additional settings section on the edit user/profile page in the admin. This section allows users to
* select a profession from a checkbox of terms from the profession taxonomy. This is just one example of
* many ways this can be handled.
*
* @param object $user The user object currently being edited.
*/
function my_edit_user_profession_section( $user ) {
$tax = get_taxonomy( 'profession' );
/* Make sure the user can assign terms of the profession taxonomy before proceeding. */
if ( !current_user_can( $tax->cap->assign_terms ) )
return;
/* Get the terms of the 'profession' taxonomy. */
$terms = get_terms( 'profession', array( 'hide_empty' => false ) ); ?>
<h3><?php _e( 'Profession' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="profession"><?php _e( 'Select Profession' ); ?></label></th>
<td><?php
/* If there are any profession terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="radio" name="profession" id="profession-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'profession', $term ) ); ?> /> <label for="profession-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
/* If there are no profession terms, display a message. */
else {
_e( 'There are no professions available.' );
}
?></td>
</tr>
</table>
<?php }
/* Update the profession terms when the edit user page is updated. */
add_action( 'personal_options_update', 'my_save_user_profession_terms' );
add_action( 'edit_user_profile_update', 'my_save_user_profession_terms' );
/**
* Saves the term selected on the edit user/profile page in the admin. This function is triggered when the page
* is updated. We just grab the posted data and use wp_set_object_terms() to save it.
*
* @param int $user_id The ID of the user to save the terms for.
*/
function my_save_user_profession_terms( $user_id ) {
$tax = get_taxonomy( 'profession' );
/* Make sure the current user can edit the user and assign terms before proceeding. */
if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$term = esc_attr( $_POST['profession'] );
/* Sets the terms (we're just using a single term) for the user. */
wp_set_object_terms( $user_id, array( $term ), 'profession', false);
clean_object_term_cache( $user_id, 'profession' );
}
/**
* Function for outputting the correct text in a tag cloud. Use as the 'update_topic_count_callback' argument
* when calling wp_tag_cloud(). Instead of 'topics' it displays 'users'.
*
* @param int $count The count of the objects for the term.
*/
function my_profession_count_text( $count ) {
return sprintf( _n('%s user', '%s users', $count ), number_format_i18n( $count ) );
}
/**
* You’d use it when displaying your tag cloud like so:
*/
wp_tag_cloud(
array(
'taxonomy' => 'profession',
'topic_count_text_callback' => 'my_profession_count_text'
)
);
/**
* Templates for term archives
*/
$term_id = get_queried_object_id();
$term = get_queried_object();
$users = get_objects_in_term( $term_id, $term->taxonomy );
if ( !empty( $users ) ) {
?>
<?php foreach ( $users as $user_id ) { ?>
<div class="user-entry">
<?php echo get_avatar( get_the_author_meta( 'email', $user_id ), '96' ); ?>
<h2 class="user-title"><a href="<?php echo esc_url( get_author_posts_url( $user_id ) ); ?>"><?php the_author_meta( 'display_name', $user_id ); ?></a></h2>
<div class="description">
<?php echo wpautop( get_the_author_meta( 'description', $user_id ) ); ?>
</div>
</div>
<?php } ?>
<?php }
/* Filter the 'sanitize_user' to disable username. */
add_filter( 'sanitize_user', 'my_disable_username' );
/**
* Disables the 'profession' username when someone registers. This is to avoid any conflicts with the custom
* 'author/profession' slug used for the 'rewrite' argument when registering the 'profession' taxonomy. This
* will cause WordPress to output an error that the username is invalid if it matches 'profession'.
*
* @param string $username The username of the user before registration is complete.
*/
function my_disable_username( $username ) {
if ( 'profession' === $username )
$username = '';
return $username;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment