Change WordPress user roles and capabilities Forums Give user access to plugin – how to Limit user to changing a single page & add events through multiple plugins

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #4804
    ArieHeij
    Participant

    Hi Vladimir,

    I have an issue with the following. I have a user which have post edit access for a few pages, and must add and edit posts with 2 other plugins. I have found some code on the this post in the forum:https://www.role-editor.com/forums/topic/limit-user-to-changing-a-single-page-add-events-through-the-event-calendar/ https://www.role-editor.com/documentation/hooks/ure_restrict_edit_post_type/ which is working great but only for one of the 2 plugins. How can this be modified for a user with 2 roles (hetbakenbeheer and kerkdienstplanning) and 2 plugin’s (event and ctc-event) it needs access to?
    Thanks.
    Kind Regards,

    Arie

    #4805
    Vladimir
    Keymaster

    Hi Arie,
    Code for user with 2 roles to exclude from the edit restrictions 2 mentioned custom post types may be similar to:

    
    add_filter('ure_restrict_edit_post_type', 'exclude_posts_from_edit_restrictions');
    function exclude_posts_from_edit_restrictions($post_type) {
      $restrict_it = true;
      $current_user = wp_get_current_user();
      if (!in_array('hetbakenbeheer', $current_user->roles) || !in_array('kerkdienstplanning', $current_user->roles)) {
        return $restrict_it;
      }
      
      $do_not_restrict = array('event', 'ctc-event');    
      if (in_array($post_type, $do_not_restrict)) {
        $restrict_it = false;
      }  
      
      return $restrict_it;
    }
    
    
    #4806
    ArieHeij
    Participant

    Hi Vladimir,

    That worked great. Thanks for the quick reply and solution. Great support.

    #4812
    ArieHeij
    Participant

    Hi Vladimir,

    I added an other filter to my functions.php for an other userrole with restrict_edit_post_type for 2 categories and full rights for ctc_sermon plugin. The problem is that for the user there are still post visible in an other categories.
    When I remove one of the functions it works fine, but when the two functions are active exclude_posts_from_edit_restrictions1 doens’t enforce the allow 2 category restriction from rol dominee.

    My code looks like this now:

    // ure_restrict_edit_post_type Hetbakenbeheren in combinatie met kerkdienstplanning
    add_filter(‘ure_restrict_edit_post_type’, ‘exclude_posts_from_edit_restrictions’);
    function exclude_posts_from_edit_restrictions($post_type) {
    $restrict_it = true;
    $current_user = wp_get_current_user();
    if (!in_array(‘hetbakenbeheren’, $current_user->roles) || !in_array(‘kerkdienstplanning’, $current_user->roles)) {
    return $restrict_it;
    }

    $do_not_restrict = array(‘event’, ‘ctc_event’);
    if (in_array($post_type, $do_not_restrict)) {
    $restrict_it = false;
    }

    return $restrict_it;
    }

    // ure_restrict_edit_post_type Dominee in combinatie met preken
    add_filter(‘ure_restrict_edit_post_type’, ‘exclude_posts_from_edit_restrictions1’);
    function exclude_posts_from_edit_restrictions1($post_type) {
    $restrict_it = true;
    if (current_user_can(‘dominee’)) {
    if ($post_type==’ctc_sermon’) {
    $restrict_it = false;
    }
    }
    return $restrict_it;
    }

    Thanks.

    #4813
    ArieHeij
    Participant

    Hi Vladimir,
    I tested some more with other user role, but as soon as post edit access is enabled for some posts / categories in combination with the filter in the function.php and there is also an filter for other user role in the function.php then the post edit access restriction is not working. You get to see all the posts instead of the posts / categories which are put in the post edit access window.
    I hope you can fix this because I have a few roles with need to have access to some posts and full access to another plugin. Thanks.

    #4814
    Vladimir
    Keymaster

    Hi Arie,

    Multiple functions on the same filter returns different results for the same custom post type as filter function does not take a result of a previously called code.

    I recommend to use a single function for this filter with a logic for all you cases. Try this variant:

    
    function exclude_posts_from_edit_restrictions($post_type) {
        $restrict_it = true;
        $current_user = wp_get_current_user();
        if (in_array($post_type, array('event', 'ctc_event'))) {
            if (in_array('hetbakenbeheren', $current_user->roles) && in_array('kerkdienstplanning', $current_user->roles)) {
                $restrict_it = false;
            }
        } elseif ($post_type=='ctc_sermon') {
            if (in_array('dominee', $current_user->roles)) {
                $restrict_it = false;
            }
        }
            
        return $restrict_it;
    }
    

    It does not restrict ‘event’ and ‘ctc_event’ CPT for a user who has both ‘hetbakenbeheren’ and ‘kerkdienstplanning’ roles at the same time.
    It does not restrict ‘ctc_sermon’ CPT if user has ‘dominee’ role.

    #4815
    ArieHeij
    Participant

    Hi Vladimir,

    Thanks for the code. I tested the code and all the the pages that are entered at the post edit access are for all the user roles visible now, but now all the posts at the other posttypes (ctc_sermon, event, ctc_event) are not showing up and stays empty for the users roles.

    #4817
    ArieHeij
    Participant

    After some more testing I found out that the user role cannot make new posts in ctc_sermon, event and ctc_event with the new code. I get the message: You do not have permission to edit this item. But the user role has all the permissions that are needed to make a new item.

    #4818
    Vladimir
    Keymaster

    Can you provide me access to this site database and theme+plugins files copy (via DropBox or similar)?
    I will setup a copy of your site at my local development environment and check what is going wrong.

    Or just provide me admin access to your site or its stage/development copy and I will get needed files/data myself (support [at-sign] role-editor.com).

    #4823
    ArieHeij
    Participant

    Hi Vladimir,

    You have a mail with the dropbox share.

    #4824
    Vladimir
    Keymaster

    Got it.
    Check your child theme functions.php file.

    1)
    Line #174 will never work:

    
       if (!current_user_can('hetbakenbeheren' or 'jaarboekje' or 'kerkdienstplanning')) {
    

    as such role this expression “‘hetbakenbeheren’ or ‘jaarboekje’ or ‘kerkdienstplanning'” is evaluated to a boolean result, but current_user_can() function wait that its argument will be a string with a role ID.
    You have to use this code:

    
    function show_full_list_of_attachments($show_all) {
       //if (!current_user_can('hetbakenbeheren' or 'jaarboekje' or 'kerkdienstplanning')) {
        $current_user = wp_get_current_user();
        if (!in_array('hetbakenbeheren', $current_user->roles) && !in_array('jaarboekje', $current_user->roles) && 
            !in_array('kerkdienstplanning', $current_user->roles)) {
         return $show_all;
       }
     
       return true;
    }
    

    In order users with one of those 3 roles see full list of attachments, but any other restricted user will still see just his own attachments in the Media Library.

    #4825
    Vladimir
    Keymaster

    2)
    You have to insert this line of code:

    
    add_filter('ure_restrict_edit_post_type', 'exclude_posts_from_edit_restrictions');
    

    just before

    
    function exclude_posts_from_edit_restrictions($post_type) {
    

    line in order this post types exclusion works. It does not work at all currently.

    Let me know the result.

    #4826
    Vladimir
    Keymaster

    That is you should have this code in your functions.php finally:

    
    // User role editor pro - Show all attachments for all roles
    add_filter('ure_attachments_show_full_list', 'show_full_list_of_attachments', 10, 1);
    function show_full_list_of_attachments($show_all) {
       //if (!current_user_can('hetbakenbeheren' or 'jaarboekje' or 'kerkdienstplanning')) {
        $current_user = wp_get_current_user();
        if (!in_array('hetbakenbeheren', $current_user->roles) && !in_array('jaarboekje', $current_user->roles) && 
            !in_array('kerkdienstplanning', $current_user->roles)) {
         return $show_all;
       }
     
       return true;
    }
    
    // ure_restrict_edit_post_type
    add_filter('ure_restrict_edit_post_type', 'exclude_posts_from_edit_restrictions');
    
    function exclude_posts_from_edit_restrictions($post_type) {
        $restrict_it = true;
        $current_user = wp_get_current_user();
        if (in_array($post_type, array('event', 'ctc_event'))) {
            if (in_array('hetbakenbeheren', $current_user->roles) && in_array('kerkdienstplanning', $current_user->roles)) {
                $restrict_it = false;
            }
        } elseif ($post_type=='ctc_sermon') {
            if (in_array('dominee', $current_user->roles)) {
                $restrict_it = false;
            }
        }
            
        return $restrict_it;
    }
    
    
    #4827
    ArieHeij
    Participant

    Hi Vladimir,

    Thanks for the support, it works great. Just for my state of mind i added another user role like this, I think I did it correct as everything worked. Can you say if I did it correctly? Thanks.

    // User role editor pro – Show all attachments for all roles
    add_filter(‘ure_attachments_show_full_list’, ‘show_full_list_of_attachments’, 10, 1);
    function show_full_list_of_attachments($show_all) {
    $current_user = wp_get_current_user();
    if (!in_array(‘hetbakenbeheren’, $current_user->roles) && !in_array(‘jaarboekje’, $current_user->roles) && !in_array(‘dominee’, $current_user->roles) &&
    !in_array(‘kerkdienstplanning’, $current_user->roles)) {
    return $show_all;
    }

    return true;
    }

    // ure_restrict_edit_post_type
    add_filter(‘ure_restrict_edit_post_type’, ‘exclude_posts_from_edit_restrictions’);
    function exclude_posts_from_edit_restrictions($post_type) {
    $restrict_it = true;
    $current_user = wp_get_current_user();
    if (in_array($post_type, array(‘event’, ‘ctc_event’))) {
    if (in_array(‘hetbakenbeheren’, $current_user->roles) && in_array(‘kerkdienstplanning’, $current_user->roles)) {
    $restrict_it = false;
    }
    } elseif ($post_type==’ctc_sermon’) {
    if (in_array(‘dominee’, $current_user->roles)) {
    $restrict_it = false;
    }
    } elseif ($post_type==’tablepress’) {
    if (in_array(‘jaarboekje’, $current_user->roles)) {
    $restrict_it = false;
    }
    }

    return $restrict_it;
    }

    #4828
    Vladimir
    Keymaster

    Yes, code was modified correctly.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.