Ask your WordPress questions! Pay money and get answers fast! (more info)

How to allow users to manage only own terms in custom taxonomy WordPress

  • SOLVED

How can I allow each user to create and 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?

This because I want that each user logged in can add his own categories only and cannot see categories added by others.

Alternatively also a solution which creates one custom taxonomy per user, accessible only by that user or by the admin, would be ok.

Any help will be appreciated. Thanks.

Answers (2)

2015-05-02

Dbranes answers:

Hi, you can try this modification to the solution by @MikeSchinkel, provided by the link of @Navjot Singh

Here I apply it for the "category" taxonomy.

/**
* Restrict categories per users.
*
* Each user can only see his own created categories (admins can see all)
*
* Modification of http://wordpress.stackexchange.com/a/8432/26350
*
*/

add_filter( 'list_terms_exclusions', 'wpq_list_terms_exclusions', 10, 3 );

function wpq_list_terms_exclusions( $exclusions, $args, $taxonomies ) {
if ( is_admin() // only modify the get_terms() calls in the backend
&& ! current_user_can( 'manage_options' ) // not for users with "admin" capability
&& current_user_can( 'manage_categories' ) // only for users that can modify categories
&& in_array( 'category', $taxonomies ) // when the taxonomy is "category"
) {
$user = wp_get_current_user();
$terms = get_user_meta( $user->ID, 'c4u', true ); // fetch the category terms created by the current users (saved in the c4u user meta value)
$part = empty( $terms ) ? ' false ' : " t.term_id IN ( {$terms} ) "; // only fetch category terms created by the current user.
$exclusions = " {$exclusions} AND {$part} ";
}
return $exclusions;
}

add_action( 'created_term', 'wpq_created_term', 10, 3 );

function wpq_created_term( $term_id, $tt_id, $taxonomy ) {
if( 'category' === $taxonomy ) { // Intercept when a "category" term is created
$user = wp_get_current_user();
if ( $user->ID ) {
$terms = get_user_meta( $user->ID,'c4u', true ); // fetch the category terms created by the current user
$terms = ( empty( $terms ) ? $term_id : "{$terms},{$term_id}" ); // we need this to be string with the CSV format
update_user_meta( $user->ID, 'c4u',$terms); // update the list of category terms created by the current user
}
}
}


Here I use the <em>created_term</em> hook instead.

Here the users can only see the categories they've self created.


Thevenin comments:

Hi, can I ask you a big pleasure? Would you be so kind to put some comments on the code to let me understand well how it works?


Thevenin comments:

Hi, and thank you for the contribute. Can I ask you a big pleasure?
Would you be so kind to add some comments so I can understand well the code and how it works? Please.


Dbranes comments:

@Thevenin,

sure, I added some (hopefully useful) comments,


Thevenin comments:

Really thank you Dbranes. You saved me.

2015-05-02

Navjot Singh answers:

Try [[LINK href="http://wordpress.stackexchange.com/questions/8428/multiple-users-only-allow-them-to-manage-their-own-terms-for-custom-taxonomy-w"]]this solution[[/LINK]].


Thevenin comments:

The problem with that solution, that I have already read, is that <strong>I don't know how to generate one taxonomy per user</strong>.

Then if I understand correctly the solution on that link will make each user logged in able to add his own categories only and unable to see others' taxonomies.

Thanks for your suggestion by the way, but it would be great to have some code to complete the answer on that link.