3

How can I allow each user to manage their own terms for a custom taxonomy when logged in on edit-tags page? user will only show & manage terms that only created by him not others for a custom taxonomy. But Admin and editor can manage all. How can I do that?

Any help will be appreciated. Thanks.

2 Answers 2

2

Thanks for the AWESOME solution MikeSchinkel :D

I just did the following updates in the code and it worked like a charm:

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);

function yoursite_list_terms_exclusions( $exclusions ) {
    $currentScreen = get_current_screen();

    if( current_user_can( 'my_custom_capability_assigned_to_specific_users' )
            && !current_user_can( 'manage_options' ) // Show everything to Admin
            && is_object( $currentScreen )
            && $currentScreen->id == 'edit-<my_taxonomy>'
            && $currentScreen->taxonomy == '<my_taxonomy>' ) {
        // Get term_id's array that you want to show as per your requirement
        $terms      = implode( ',', $term_id );
        $exclusions = ( empty( $exclusions ) ? '' : $exclusions ) . ' AND' . ' t.`term_id` IN (' . $terms . ')';
    }
    return $exclusions;
}
3

Hmmm. You are going to need to keep track of who added which terms which you can currently best do in a usermeta field as a comma-separated list (I'm assuming you don't expect more than ~1000 terms per user? At lot more than then I'd worry.)

You can store the term IDs in the usermeta field using the 'edited_terms' hook as you see here:

Basically your code might look like this"

add_filter('edited_terms', 'yoursite_edited_terms');
function yoursite_edited_terms($term_id) {
  if($_POST['taxonomy'] == 'your_taxonomy'): {
    $user = wp_get_current_user();
    if ($user->ID) {
      $terms = get_user_meta($user->ID,'users_terms_for_your_taxonomy');
      $terms = (empty($terms) ? $term_id : "{$terms},{$term_id}");
      update_user_meta($user->ID,'users_terms_for_your_taxonomy',$terms);
    }
  }
}

Then you'll need to use the 'list_terms_exclusions' hook to limit your terms to only the ones needed for the current user, but allowing the Administrator an Editor to see them all by adding a 'can_manage_all_terms' capability which you can use the Members plugin to create:

Here's what that might look like:

add_filter('list_terms_exclusions', 'yoursite_list_terms_exclusions', 10, 2);
function yoursite_list_terms_exclusions( $exclusions, $args ) {
  global $pagenow;
  if (current_user_can('can_manage_all_terms') && 
    $pagenow=='edit-tags.php?taxonomy=your_post_taxonomy') {
    $terms = get_user_meta($user->ID,'users_terms_for_your_taxonomy');
    $exclusions = " {$exclusions} AND t.ID NOT IN ({$terms})";
  }
  return $exclusions;
}

I haven't tested this code but it should get you on the right track. And be sure to replace anything in the code written as 'your_taxonomy' with a reference to your actual taxonomy.

6
  • @mike - i had done this before but i keep track by storing taxonomy_term-id[user_id] in the options table, do you think keeping the data in usermeta is better?
    – Bainternet
    Feb 3, 2011 at 11:45
  • בניית אתרים - the options table is not bad either, but the problem is we have no taxonomy meta which is where I'd want to store it. I do like usermeta because at least it's one-to-one for the user. In reality, no solution we have available to us (yet) is perfect. Feb 3, 2011 at 12:01
  • @MikeSchinkel Thanks for your reply. The first part of the code is not working. The user meta is not updating. Will you check?
    – mha
    Feb 3, 2011 at 12:59
  • @mha - Have you tried to debug any of it using var_dump() statements at various points in the function? (I should know better than try to answer when I don't have the time to actually test the code...) Feb 3, 2011 at 13:09
  • @MikeSchinkel I have tried but the code does not work.
    – mha
    Feb 9, 2011 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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