Support » Plugins » Hacks » Applying custom taxonomies to user profiles

  • The goal is to taxonomize registered user profiles using custom taxonomies, whether hierarchical or not. I couldn’t find an out-of-the-box way to do it, so I’m trying to pull a custom solution myself.

    First of all, I created the custom taxonomies I needed. Let’s say “Gender” and “Country”.

    Second, I added a single custom profile user field to store all term IDs, regardless of the taxonomy they belong to (there’s no duplicates between terms, so I can safely do this). Let’s call this field “selected”.

    Third, I created a function to show the terms of a taxonomy as a set of checkboxes, checking by default the ones which IDs are stored in our custom profile user field “selected”:

    function term2checks($taxonomy,$userID) {
    
       //Get the selected terms for the current user
       $selected = ','.get_the_author_meta( 'selected', $userID ).',';
    
       //Query the database to get the terms
       global $wpdb;
       $query = "SELECT wt.term_id, wt.name
                 FROM $wpdb->term_taxonomy wtt
                 LEFT JOIN $wpdb->terms wt ON wtt.term_id = wt.term_id
                 WHERE wtt.taxonomy = '".$taxonomy."'
                 ORDER BY wt.name";
       $terms = $wpdb->get_results($query);
    
       //Create a set of checkboxes with those terms
       $checkboxes = '';
       foreach($terms as $term) {
          $checkboxes .= '<input type="checkbox" name="'.$taxonomy.'[]" id="'.$taxonomy.'[]" value="'.$term->term_id.'"';
          //If the term is currently selected, check
          if (strstr($selected,','.$term->term_id.',')) {
             $checkboxes .= ' checked="checked"';
          }
          $checkboxes .= '/>'.$term->name.'<br />';
       }
       return $checkboxes;
    }

    Fourth, I added to the user profile admin page a set of checkboxes for each taxonomy I wanted to apply. I used Justin’s functions (check the link before on custom profile fields) and modified them a bit:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    function my_show_extra_profile_fields( $user ) { ?>
    
       <h3>Taxonomies</h3>
    
       <table class="form-table">
    
          <tr>
             <th><label for="gender">Gender</label></th>
             <td>
                <?php echo term2checks('gender',$user->ID);
                <span class="description">The gender of the user.</span>
             </td>
          </tr>
    
          <tr>
             <th><label for="country">Country</label></th>
             <td>
                <?php echo term2checks('country',$user->ID);
                <span class="description">The country where the user comes from.</span>
             </td>
          </tr>
    
       </table>
    <?php }

    Fifth, I collected the data from all sets of checkboxes and saved it in our custom user profile fields. Again, I modified one of Justin’s functions:

    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
       if ( !current_user_can( 'edit_user', $user_id ) )
          return false;
       update_usermeta( $user_id, 'selected', implode(',',$_POST['gender']).','.implode(',',$_POST['country']) );
    }

    And last, I used this query to get users filtered by taxonomies. In the example, I’m getting users with gender id 1 and country id 10.

    SELECT user_id
       FROM wp_usermeta
       WHERE meta_key = 'selected'
       AND FIND_IN_SET(1,meta_value)
       AND FIND_IN_SET(10,meta_value)

    Of course, you could join other tables in order to get display names or to order by other criteria.

    What do you think of this approach? Would it work in a site with lots of traffic? Can it be improved somehow?

    Feel free to say what you think, I’m here to learn 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • I think this looks great, but I a can’t speak to how scalable it is!

    Thread Starter

    (@rickjoe)

    Thanks for the response! I was about to lose all hope 😛

    Well, I tried it in a test environment and it does work fine, but I’m stuck at the same point you are, I don’t know how this would behave in a large live environment and I don’t have the resources to try it before implementing it in a real site.

    I guess this isn’t a primary interest for the WordPress community, but I still hope to find anyone else who is trying to accomplish this same thing so we can join efforts 🙂

    Wow. This is pretty much exactly what I’m trying to do… Do you have this working somewhere that I can look at it.

    Here’s basically what I’m trying to do (apologies, because this is a cut and paste from a post on Wpmudev.com where I posted this question earlier)

    I think we are trying to accomplish the same thing.

    Basically, I’m setting up a WP site for the student government at my college. For the most part it’s a pretty straight forward setup, but I’m stuck on figuring out how to get multiple authors to span multiple administrations.

    So if we’ve got a President, Secretary and a Vice President (just an example, we actually have a few more) and we want posts to have some author meta like: Tommy Smith, Vice President

    So this is no problem for one year, we could easily add a field to the user profile. But let’s say next year, the Vice President is Samantha Jones. In this case we’d want to be able to link up (automagically) both the person’s name (which would give an archive of posts that they personally posted) or the their title, Vice President (which would show an archive of everything posted by any Vice President).

    To me it’s like adding a special tag to user profiles. Another guy in our shop thought of it more like custom roles and being able to query against those roles.

    Does this make sense? Is this sort of thing possible? Any thoughts or direction would be much appreciated.

    Thread Starter

    (@rickjoe)

    theryancollier,

    I’ve been digging into this and it turns out that, theoretically, the EAV structure I’m proposing here isn’t quite efficient, so it wouldn’t be the most optimum solution when it comes to large scenarios; but the wide known social plugin BuddyPress uses this same model and as far as I know it works fine. Even though, I’ve been looking into the way WP stores standard post taxonomization data (which is theoretically more efficient) and apparently it could be applied to users as well. If I get to anything worth sharing, I’ll post it here as soon as possible.

    Regarding your case, I wouldn’t use custom roles: they control WP permissions, and those aren’t related with government jobs. I would go for the special tag, that way you can assign site admins/editors and government positions separately.

    You’re telling me that you have only a few government positions, and I guess they don’t change much. If that’s the case, you don’t even need to mess with taxonomies: with this code and Justin Tadlock’s post on the subject you should figure out how to go on. Just a little consideration:

    1) Tommy Smith is Vice President
    2) Tommy Smith publishes post 1, which says “Tommy Smith, Vice President”
    3) Tommy Smith becomes President, Samantha Jones becomes Vice President
    4) Post 1 now says “Tommy Smith, President”

    I mean, getting the government position from the user meta will show the current position in every post. If you want posts to show the position the author had when he published it, you should save it in posts’ custom fields.

    @ricardo, @theryancollier
    Hey guys, did any of you find a working solution?

    (@disonancias)

    Nicusor,

    Like I said before, the EAV modeling I was proposing isn’t efficient with large databases. A better solution would require to alter WP core tables, but I strongly advice against that, mostly because of standards and compatibility. Finally, I came up with a whole different approach for that specific project (that’s why I never posted a solution here, because it has nothing to do with the original idea).

    If you’re building something that may require custom tables, perhaps you should take a look to the Pods CMS plugin. Feel free to ask about your specific needs, I’ll help you as much as I can from here.

    Ricardo,

    Actually, looking for a solution, I also came across Pods CMS plugin … It’s an interesting framework, I must analyze it more closely before taking a decision…
    If you built a site with Pods, and want to share, I’d like to see it in action 🙂

    Woothemes has a different approach to…
    Custom Fields on a Custom Taxonomy (Author)
    I’m gonna try this to, but first I have to put my site structure in order… it’s gating pretty complex…

    It’s the first time for me when I try to use WP as a real CMS platform, it has some certain limitations, but I’m pretty impressed so far. To make it work I will probably need to use some duplicates “fields”…

    Thank you for your help Ricardo

    (@disonancias)

    Nicusor,

    I’ve been playing with Pods, but haven’t used them in live sites. I’m a bit reluctant to use them because many of their functionalities can be accomplished using core features (i.e. custom post types). WordPress itself has become a quite impressive CMS platform, and I think that’s better to use plugins as less as possible, because that way you have less developers to rely on in order to have updated software.

    At this point I guess you found out that there isn’t only one correct answer, you can do the same thing in many ways. Let me know if you need a hand with that structure.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Applying custom taxonomies to user profiles’ is closed to new replies.