Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Problems creating new post with user having category/taxonomy ID restriction › Reply To: Problems creating new post with user having category/taxonomy ID restriction
21/02/2016 at 11:40
#2016
Biranit Goren
Participant
Hi Vladimir,
I realized later that I actually have a conceptual mistake: if all users get the same term_id set — then all users will be able to edit other users’ posts 🙂
So instead, I am using the user’s user_id as the term, and my functions look as follows:
function rgb_allow_user_posts($post_id, $post, $update) {
global $pagenow, $wpdb;
if ($pagenow !=='post-new.php') { // for new added post only
return;
}
$user_id = get_current_user_id();
$terms_list_str = get_user_meta($user_id, 'wp_ure_categories_list', true);
if (empty($terms_list_str)) {
return;
}
$restriction_type = get_user_meta($user_id, 'wp_ure_posts_restriction_type', true);
if ($restriction_type!=1) { // allow
return;
}
wp_set_object_terms( $post_id, strval($user_id), 'ure', false);
}
add_filter('wp_insert_post', 'rgb_allow_user_posts', 10, 3);
function rgb_add_user_term($user_id) {
$organizations = get_user_meta($user_id, 'wp_ure_categories_list', true);
if ($organizations!='' && get_user_meta($user_id, 'wp_ure_posts_restriction_type', true)=='1') {
$user_term = get_term_by( 'name', $user_id, 'ure');
$vals = explode(', ', $organizations);
if (!in_array($user_term->term_id, $vals)) {
$organizations = '$user_term->term_id . ', ' . $organizations;
update_user_meta($user_id, 'wp_ure_categories_list', $organizations);
}
}
}
add_action('profile_update', 'rgb_add_user_term', 99);
Hope this helps…
Thanks,
Bira