Advertisement
Guest User

Two custom user taxonomies for Wordpress

a guest
May 30th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.12 KB | None | 0 0
  1. add_action( 'init', 'my_register_user_taxonomy' );
  2. /**
  3.  * Registers the 'profession' taxonomy for users.  This is a taxonomy for the 'user' object type rather than a
  4.  * post being the object type.
  5.  */
  6. function my_register_user_taxonomy() {
  7.  
  8.      register_taxonomy(
  9.         'profession',
  10.         'user',
  11.         array(
  12.             'public' => true,
  13.             'labels' => array(
  14.                 'name' => __( 'Professions' ),
  15.                 'singular_name' => __( 'Profession' ),
  16.                 'menu_name' => __( 'Professions' ),
  17.                 'search_items' => __( 'Search Professions' ),
  18.                 'popular_items' => __( 'Popular Professions' ),
  19.                 'all_items' => __( 'All Professions' ),
  20.                 'edit_item' => __( 'Edit Profession' ),
  21.                 'update_item' => __( 'Update Profession' ),
  22.                 'add_new_item' => __( 'Add New Profession' ),
  23.                 'new_item_name' => __( 'New Profession Name' ),
  24.                 'separate_items_with_commas' => __( 'Separate professions with commas' ),
  25.                 'add_or_remove_items' => __( 'Add or remove professions' ),
  26.                 'choose_from_most_used' => __( 'Choose from the most popular professions' ),
  27.             ),
  28.             'rewrite' => array(
  29.                 'with_front' => true,
  30.                 'slug' => 'author/profession' // Use 'author' (default WP user slug).
  31.             ),
  32.             'capabilities' => array(
  33.                 'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
  34.                 'edit_terms'   => 'edit_users',
  35.                 'delete_terms' => 'edit_users',
  36.                 'assign_terms' => 'read',
  37.             ),
  38.             'update_count_callback' => 'my_update_profession_count' // Use a custom function to update the count.
  39.         )
  40.     );
  41.  
  42.     register_taxonomy(
  43.         'office',
  44.         'user',
  45.         array(
  46.             'public' => true,
  47.             'labels' => array(
  48.                 'name' => __( 'Offices' ),
  49.                 'singular_name' => __( 'office' ),
  50.                 'menu_name' => __( 'offices' ),
  51.                 'search_items' => __( 'Search offices' ),
  52.                 'popular_items' => __( 'Popular offices' ),
  53.                 'all_items' => __( 'All offices' ),
  54.                 'edit_item' => __( 'Edit office' ),
  55.                 'update_item' => __( 'Update office' ),
  56.                 'add_new_item' => __( 'Add New office' ),
  57.                 'new_item_name' => __( 'New office Name' ),
  58.                 'separate_items_with_commas' => __( 'Separate offices with commas' ),
  59.                 'add_or_remove_items' => __( 'Add or remove offices' ),
  60.                 'choose_from_most_used' => __( 'Choose from the most popular offices' ),
  61.             ),
  62.             'rewrite' => array(
  63.                 'with_front' => true,
  64.                 'slug' => 'author/office' // Use 'author' (default WP user slug).
  65.             ),
  66.             'capabilities' => array(
  67.                 'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
  68.                 'edit_terms'   => 'edit_users',
  69.                 'delete_terms' => 'edit_users',
  70.                 'assign_terms' => 'read',
  71.             ),
  72.             'update_count_callback' => 'my_update_office_count' // Use a custom function to update the count.
  73.         )
  74.     );
  75. }
  76.  
  77. /**
  78.  * Function for updating the 'profession' taxonomy count.  What this does is update the count of a specific term
  79.  * by the number of users that have been given the term.  We're not doing any checks for users specifically here.
  80.  * We're just updating the count with no specifics for simplicity.
  81.  *
  82.  * See the _update_post_term_count() function in WordPress for more info.
  83.  *
  84.  * @param array $terms List of Term taxonomy IDs
  85.  * @param object $taxonomy Current taxonomy object of terms
  86.  */
  87. function my_update_profession_count( $terms, $taxonomy ) {
  88.     global $wpdb;
  89.  
  90.     foreach ( (array) $terms as $term ) {
  91.  
  92.         $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
  93.  
  94.         do_action( 'edit_term_taxonomy', $term, $taxonomy );
  95.         $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
  96.         do_action( 'edited_term_taxonomy', $term, $taxonomy );
  97.     }
  98. }
  99.  
  100. // And offices
  101. function my_update_office_count( $terms, $taxonomy ) {
  102.     global $wpdb;
  103.  
  104.     foreach ( (array) $terms as $term ) {
  105.  
  106.         $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
  107.  
  108.         do_action( 'edit_term_taxonomy', $term, $taxonomy );
  109.         $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
  110.         do_action( 'edited_term_taxonomy', $term, $taxonomy );
  111.     }
  112. }
  113.  
  114.  
  115.  
  116. /* Adds the taxonomy page in the admin. */
  117. add_action( 'admin_menu', 'my_add_profession_admin_page' );
  118. function my_add_profession_admin_page() {
  119.  
  120.     $tax = get_taxonomy( 'profession' );
  121.  
  122.     add_users_page(
  123.         esc_attr( $tax->labels->menu_name ),
  124.         esc_attr( $tax->labels->menu_name ),
  125.         $tax->cap->manage_terms,
  126.         'edit-tags.php?taxonomy=' . $tax->name
  127.     );
  128. }
  129. // Aaaand offices
  130. add_action( 'admin_menu', 'my_add_office_admin_page' );
  131. function my_add_office_admin_page() {
  132.  
  133.     $tax = get_taxonomy( 'office' );
  134.  
  135.     add_users_page(
  136.         esc_attr( $tax->labels->menu_name ),
  137.         esc_attr( $tax->labels->menu_name ),
  138.         $tax->cap->manage_terms,
  139.         'edit-tags.php?taxonomy=' . $tax->name
  140.     );
  141. }
  142.  
  143.  
  144.  
  145. /* Create custom columns for the manage profession page. */
  146. add_filter( 'manage_edit-profession_columns', 'my_manage_profession_user_column' );
  147.  
  148. /**
  149.  * Unsets the 'posts' column and adds a 'users' column on the manage profession admin page.
  150.  *
  151.  * @param array $columns An array of columns to be shown in the manage terms table.
  152.  */
  153. function my_manage_profession_user_column( $columns ) {
  154.  
  155.     unset( $columns['posts'] );
  156.  
  157.     $columns['users'] = __( 'Users' );
  158.  
  159.     return $columns;
  160. }
  161.  
  162. /* Create custom columns for the manage office page. */
  163. add_filter( 'manage_edit-office_columns', 'my_manage_office_user_column' );
  164. function my_manage_office_user_column( $columns ) {
  165.     unset( $columns['posts'] );
  166.     $columns['users'] = __( 'Users' );
  167.     return $columns;
  168. }
  169.  
  170. /* Customize the output of the custom column on the manage professions page. */
  171. add_action( 'manage_profession_custom_column', 'my_manage_profession_column', 10, 3 );
  172.  
  173. /**
  174.  * Displays content for custom columns on the manage professions page in the admin.
  175.  *
  176.  * @param string $display WP just passes an empty string here.
  177.  * @param string $column The name of the custom column.
  178.  * @param int $term_id The ID of the term being displayed in the table.
  179.  */
  180. function my_manage_profession_column( $display, $column, $term_id ) {
  181.  
  182.     if ( 'users' === $column ) {
  183.         $term = get_term( $term_id, 'profession' );
  184.         echo $term->count;
  185.     }
  186. }
  187. /* Customize the output of the custom column on the manage professions page. */
  188. add_action( 'manage_office_custom_column', 'my_manage_office_column', 10, 3 );
  189. function my_manage_office_column( $display, $column, $term_id ) {
  190.  
  191.     if ( 'users' === $column ) {
  192.         $term = get_term( $term_id, 'office' );
  193.         echo $term->count;
  194.     }
  195. }
  196.  
  197.  
  198. /* Add section to the edit user page in the admin to select profession. */
  199. add_action( 'show_user_profile', 'my_edit_user_profession_section' );
  200. add_action( 'edit_user_profile', 'my_edit_user_profession_section' );
  201.  
  202. /**
  203.  * Adds an additional settings section on the edit user/profile page in the admin.  This section allows users to
  204.  * select a profession from a checkbox of terms from the profession taxonomy.  This is just one example of
  205.  * many ways this can be handled.
  206.  *
  207.  * @param object $user The user object currently being edited.
  208.  */
  209. function my_edit_user_profession_section( $user ) {
  210.  
  211.     $tax = get_taxonomy( 'profession' );
  212.  
  213.     /* Make sure the user can assign terms of the profession taxonomy before proceeding. */
  214.     if ( !current_user_can( $tax->cap->assign_terms ) )
  215.         return;
  216.  
  217.     /* Get the terms of the 'profession' taxonomy. */
  218.     $terms = get_terms( 'profession', array( 'hide_empty' => false ) ); ?>
  219.  
  220.     <h3><?php _e( 'Profession' ); ?></h3>
  221.  
  222.     <table class="form-table">
  223.  
  224.         <tr>
  225.             <th><label for="profession"><?php _e( 'Select Profession' ); ?></label></th>
  226.  
  227.             <td><?php
  228.  
  229.             /* If there are any profession terms, loop through them and display checkboxes. */
  230.             if ( !empty( $terms ) ) {
  231.  
  232.                 foreach ( $terms as $term ) { ?>
  233.                     <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 />
  234.                 <?php }
  235.             }
  236.  
  237.             /* If there are no profession terms, display a message. */
  238.             else {
  239.                 _e( 'There are no professions available.' );
  240.             }
  241.  
  242.             ?></td>
  243.         </tr>
  244.  
  245.     </table>
  246. <?php }
  247.  
  248.  
  249. add_action( 'show_user_profile', 'my_edit_user_office_section' );
  250. add_action( 'edit_user_profile', 'my_edit_user_office_section' );
  251.  
  252. /**
  253.  * Adds an additional settings section on the edit user/profile page in the admin.  This section allows users to
  254.  * select a profession from a checkbox of terms from the profession taxonomy.  This is just one example of
  255.  * many ways this can be handled.
  256.  *
  257.  * @param object $user The user object currently being edited.
  258.  */
  259. function my_edit_user_office_section( $user ) {
  260.  
  261.     $tax = get_taxonomy( 'office' );
  262.  
  263.     /* Make sure the user can assign terms of the profession taxonomy before proceeding. */
  264.     if ( !current_user_can( $tax->cap->assign_terms ) )
  265.         return;
  266.  
  267.     /* Get the terms of the 'profession' taxonomy. */
  268.     $terms = get_terms( 'office', array( 'hide_empty' => false ) ); ?>
  269.  
  270.     <h3><?php _e( 'Office' ); ?></h3>
  271.  
  272.     <table class="form-table">
  273.  
  274.         <tr>
  275.             <th><label for="office"><?php _e( 'Select Office' ); ?></label></th>
  276.  
  277.             <td><?php
  278.  
  279.             /* If there are any profession terms, loop through them and display checkboxes. */
  280.             if ( !empty( $terms ) ) {
  281.  
  282.                 foreach ( $terms as $term ) { ?>
  283.                     <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 />
  284.                 <?php }
  285.             }
  286.  
  287.             /* If there are no profession terms, display a message. */
  288.             else {
  289.                 _e( 'There are no offices available.' );
  290.             }
  291.  
  292.             ?></td>
  293.         </tr>
  294.  
  295.     </table>
  296. <?php }
  297.  
  298.  
  299.  
  300. /* Update the profession terms when the edit user page is updated. */
  301. add_action( 'personal_options_update', 'my_save_user_profession_terms' );
  302. add_action( 'edit_user_profile_update', 'my_save_user_profession_terms' );
  303.  
  304. /**
  305.  * Saves the term selected on the edit user/profile page in the admin. This function is triggered when the page
  306.  * is updated.  We just grab the posted data and use wp_set_object_terms() to save it.
  307.  *
  308.  * @param int $user_id The ID of the user to save the terms for.
  309.  */
  310. function my_save_user_profession_terms( $user_id ) {
  311.  
  312.     $tax = get_taxonomy( 'profession' );
  313.  
  314.     /* Make sure the current user can edit the user and assign terms before proceeding. */
  315.     if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
  316.         return false;
  317.  
  318.     $term = $_POST['profession'] ;
  319.  
  320.     /* Sets the terms (we're just using a single term) for the user. */
  321.     wp_set_object_terms( $user_id, $term, 'profession', false);
  322.  
  323.     clean_object_term_cache( $user_id, 'profession' );
  324. }
  325.  
  326. add_action( 'personal_options_update', 'my_save_user_office_terms' );
  327. add_action( 'edit_user_profile_update', 'my_save_user_office_terms' );
  328.  
  329. /**
  330.  * Saves the term selected on the edit user/profile page in the admin. This function is triggered when the page
  331.  * is updated.  We just grab the posted data and use wp_set_object_terms() to save it.
  332.  *
  333.  * @param int $user_id The ID of the user to save the terms for.
  334.  */
  335. function my_save_user_office_terms( $user_id ) {
  336.  
  337.     $tax = get_taxonomy( 'office' );
  338.  
  339.     /* Make sure the current user can edit the user and assign terms before proceeding. */
  340.     if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
  341.         return false;
  342.  
  343.     $term = $_POST['office'] ;
  344.  
  345.     /* Sets the terms (we're just using a single term) for the user. */
  346.     wp_set_object_terms( $user_id, $term, 'office', false);
  347.  
  348.     clean_object_term_cache( $user_id, 'office' );
  349. }
  350.  
  351.  
  352. /* Filter the 'sanitize_user' to disable username. */
  353. add_filter( 'sanitize_user', 'my_disable_username' );
  354.  
  355. /**
  356.  * Disables the 'profession' username when someone registers.  This is to avoid any conflicts with the custom
  357.  * 'author/profession' slug used for the 'rewrite' argument when registering the 'profession' taxonomy.  This
  358.  * will cause WordPress to output an error that the username is invalid if it matches 'profession'.
  359.  *
  360.  * @param string $username The username of the user before registration is complete.
  361.  */
  362. function my_disable_username( $username ) {
  363.  
  364.     if ( 'profession' === $username )
  365.         $username = '';
  366.  
  367.     return $username;
  368. }
  369.  
  370.  
  371. add_filter( 'parent_file', 'fix_user_profession_page' );
  372.  
  373. function fix_user_profession_page( $parent_file = '' ) {
  374.     global $pagenow;
  375.  
  376.     if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'profession' && $pagenow == 'edit-tags.php' ) {
  377.         $parent_file = 'users.php';
  378.     }
  379.  
  380.     return $parent_file;
  381. }
  382.  
  383. add_filter( 'parent_file', 'fix_user_office_page' );
  384.  
  385. function fix_user_office_page( $parent_file = '' ) {
  386.     global $pagenow;
  387.  
  388.     if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'office' && $pagenow == 'edit-tags.php' ) {
  389.         $parent_file = 'users.php';
  390.     }
  391.  
  392.     return $parent_file;
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement