8

I've set up my user profiles to allow custom taxonomies following this tutorial.

Ideally, I'd like to use the default Tag meta box from the Post screen, so users can easily add new terms and find commonly used ones. I'm currently using a checkbox list, but this could get very long as we'll have a number of taxonomies. Could I somehow use the post_tags_meta_box()? I've looked into add_meta_box() but that doesn't work on 'user'.

  • 2
    +1 for that link... didn't know you could add taxonomies to users! – Michael Lewis Aug 5 '13 at 22:08
  • Could you please actually show what you've tried? I mean "show with your code that's not working". – kaiser Aug 5 '13 at 22:18
  • @kaiser The only code I really tried was add_meta_box() but it can only be used on post types, not the user object. – epschmidt Aug 6 '13 at 14:08
  • As a sidenote, taxonomies can be added to anything that has an ID, including comments, users, posts, or even taxonomies themselves. This si because taxonomy terms relate IDs but don't make any assumption about what that ID represents beyond a data format. It's the functions on top that make those assumptions – Tom J Nowell Aug 15 '14 at 17:03
1

Unfortunately, I'd say the answer is no.

You could hijack (copy + paste) the code from post_categories_meta_box() and try to adapt it to the user, however you'd spend more time trying to reconnect all the pieces. I'm pretty sure the post meta boxes use ajax to save and add new terms, so you'd have to hijack some javascript and modify that as well.

I think your best bet is to continue down the path you're on. You can add an overflow:scroll; css property to your checkbox container so that it doesn't get too long.

I reversed engineered the post meta boxes for the front end a few months ago, and it wasn't too hard. I wouldn't suggest trying to reuse the markup and php functions from the core though. Instead, use them as a guide if you get stuck.

  • I went down the path of trying to pull the code from the Tag box in post_tags_meta_box() from the core and add it to the user profile. But you're right, there's some javascript and ajax stuff going on that I'd need to copy over as well which would make this more complicated than I need. I think I'll just create a 'Member' custom post type and publish a post when a user is registered and use taxonomies with that instead. – epschmidt Aug 6 '13 at 14:11
1

Brief answer to only sketch out what needs to be done:

  1. WP uses javascript to add all the magic to the meta boxes (drag & drop, folding, etc.) -> You'll need to wp_enqueue_script( 'posts' ); (or however that script handle was called).
  2. You'll need the hooks that allow registering the meta boxes.
  3. There's specific MarkUp (mostly containers and such) that needs to get rendered on the user profile screen.
1

This code works for me. It uses 'locations' custom taxonomy and 'suggest' javascript. You need to extend it to support multiple term selection.

Add custom field to user-edit screen and store metadata when user/admin updates profile

// for account owner
add_action('show_user_profile', 'add_custom_user_profile_fields');
add_action('personal_options_update', 'save_custom_user_profile_fields');

// for admins
add_action('edit_user_profile', 'add_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');

function add_custom_user_profile_fields($user) {
    printf(
    '
<h3>%1$s</h3>
<table class="form-table">
<tr>
<th><label for="location">%2$s</label></th>
<td>
  <input type="text" name="location" id="location" value="%3$s" class="regular-text" />
  <br /><span class="description">%4$s</span>
</td>
</tr>
</table>
',      __('Extra Profile Information', 'locale'),
        __('Location', 'locale'),
        esc_attr(get_user_meta($user->ID, 'location', true)),
        __('Start typing location name.', 'locale')
    );
}

function save_custom_user_profile_fields($user_id) {
    if (!current_user_can('edit_user', $user_id))
        return FALSE;

    $location_name = ( isset($_POST['location']) ) ? $_POST['location'] : '';

    // use your taxonomy name instead of 'locations'
    $location = get_term_by('name', $location_name, 'locations');

    // human readable value and id
    update_user_meta($user_id, 'location', $location_name);
    update_user_meta($user_id, 'location_id', $location->term_id);
}

Enqueue suggest javascript for user-edit screen only (assuming you use this in custom theme)

function admin_scripts($hook) {
    $screen = get_current_screen();
    if ('user-edit' == $screen->id) {
    wp_enqueue_script(
        'user-edit-tag',
        get_stylesheet_directory_uri() . '/js/usermeta.js',
        array('suggest'),
        '20140509',
        true
    );
    }
}

usermeta.js

jQuery(document).ready(function($) {
   // use 'tax=your_taxonomy_name' instead of 'tax=locations'
   $('#location').suggest(ajaxurl+"?action=ajax-tag-search&tax=locations",{
        multiple:false,
        multipleSep: ","
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.