Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    In normal WordPress use, you don’t check for roles, you check for capabilities. If you want to check if someone is at least an editor, you check current_user_can( 'edit_others_posts' ). So, something like this:

    add_filter( 'relevanssi_post_ok', 'rlv_member_check', 10, 2 );

    function rlv_member_check( $post_ok, $post_id ) {
    // An array of post IDs that only editors can access.
    // This can be created manually like this or automatically, whatever is your criteria.
    $editor_posts = array( 1, 2, 3, 4 );

    $post = get_post( $post_id );
    if ( in_array( $post_id, $editor_posts ) && ! current_user_can( 'edit_others_posts ' ) ) {
    // Post is among the editor posts, and the user is not an editor.
    $post_ok = false;
    }
    return $post_ok;
    }

    Now, I don’t know Members affects this. If it just offers an easy way to extend the WordPress roles and capabilities system, you’d do it like this, but if it’s something else, then you need to replace current_user_can() with whatever works with Members. The Members support forums will help you with that.

    Thread Starter tomz6

    (@tomz6)

    For the user roles on my website, some of them have the same capabilities but we have restricted certain pages to certain user roles (so I need to check for roles instead of capabilities).

    Please let me know if it’s possible to check for roles instead of capabilities.

    • This reply was modified 3 weeks, 4 days ago by tomz6.
    Plugin Author Mikko Saari

    (@msaari)

    It’s definitely not easy – the WordPress way is to check capabilities, not roles. It would probably be easiest if you could create a new specific capability for each role just for the sake of checking the role.

    The WP_User object, for example, has methods for adding a role, removing a role and setting user roles, but no method for checking the user role. The only method is get_role_caps(), which returns all the capabilities the user has from their roles.

    In any case, this no longer has anything to do with Relevanssi. I recommend you ask a wider audience – try your luck on more general WP support forums, perhaps someone can give you a good method for checking user roles. I don’t know one – I’ve always used capabilities because that’s easy.

    Thread Starter tomz6

    (@tomz6)

    Many thanks for the help. I just created new capability for each role.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.