Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'init', 'my_register_user_taxonomy' );
- /**
- * 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.
- )
- );
- register_taxonomy(
- 'office',
- 'user',
- array(
- 'public' => true,
- 'labels' => array(
- 'name' => __( 'Offices' ),
- 'singular_name' => __( 'office' ),
- 'menu_name' => __( 'offices' ),
- 'search_items' => __( 'Search offices' ),
- 'popular_items' => __( 'Popular offices' ),
- 'all_items' => __( 'All offices' ),
- 'edit_item' => __( 'Edit office' ),
- 'update_item' => __( 'Update office' ),
- 'add_new_item' => __( 'Add New office' ),
- 'new_item_name' => __( 'New office Name' ),
- 'separate_items_with_commas' => __( 'Separate offices with commas' ),
- 'add_or_remove_items' => __( 'Add or remove offices' ),
- 'choose_from_most_used' => __( 'Choose from the most popular offices' ),
- ),
- 'rewrite' => array(
- 'with_front' => true,
- 'slug' => 'author/office' // 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_office_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 );
- }
- }
- // And offices
- function my_update_office_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 );
- }
- }
- /* Adds the taxonomy page in the admin. */
- add_action( 'admin_menu', 'my_add_profession_admin_page' );
- 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
- );
- }
- // Aaaand offices
- add_action( 'admin_menu', 'my_add_office_admin_page' );
- function my_add_office_admin_page() {
- $tax = get_taxonomy( 'office' );
- 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;
- }
- /* Create custom columns for the manage office page. */
- add_filter( 'manage_edit-office_columns', 'my_manage_office_user_column' );
- function my_manage_office_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;
- }
- }
- /* Customize the output of the custom column on the manage professions page. */
- add_action( 'manage_office_custom_column', 'my_manage_office_column', 10, 3 );
- function my_manage_office_column( $display, $column, $term_id ) {
- if ( 'users' === $column ) {
- $term = get_term( $term_id, 'office' );
- 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="checkbox" 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 }
- add_action( 'show_user_profile', 'my_edit_user_office_section' );
- add_action( 'edit_user_profile', 'my_edit_user_office_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_office_section( $user ) {
- $tax = get_taxonomy( 'office' );
- /* 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( 'office', array( 'hide_empty' => false ) ); ?>
- <h3><?php _e( 'Office' ); ?></h3>
- <table class="form-table">
- <tr>
- <th><label for="office"><?php _e( 'Select Office' ); ?></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="checkbox" name="office[]" id="office-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'office', $term ) ); ?> /> <label for="office-<?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 offices 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 = $_POST['profession'] ;
- /* Sets the terms (we're just using a single term) for the user. */
- wp_set_object_terms( $user_id, $term, 'profession', false);
- clean_object_term_cache( $user_id, 'profession' );
- }
- add_action( 'personal_options_update', 'my_save_user_office_terms' );
- add_action( 'edit_user_profile_update', 'my_save_user_office_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_office_terms( $user_id ) {
- $tax = get_taxonomy( 'office' );
- /* 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 = $_POST['office'] ;
- /* Sets the terms (we're just using a single term) for the user. */
- wp_set_object_terms( $user_id, $term, 'office', false);
- clean_object_term_cache( $user_id, 'office' );
- }
- /* 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;
- }
- add_filter( 'parent_file', 'fix_user_profession_page' );
- function fix_user_profession_page( $parent_file = '' ) {
- global $pagenow;
- if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'profession' && $pagenow == 'edit-tags.php' ) {
- $parent_file = 'users.php';
- }
- return $parent_file;
- }
- add_filter( 'parent_file', 'fix_user_office_page' );
- function fix_user_office_page( $parent_file = '' ) {
- global $pagenow;
- if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'office' && $pagenow == 'edit-tags.php' ) {
- $parent_file = 'users.php';
- }
- return $parent_file;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement