BBPress help

4 Replies ·

  1. Hi Justin,

    Part #1:

    I am working on a project, using BBPress, and I am also using Members plugin for handling roles and capabilities.

    I need to add a capability to Participants, so they can upload Media, but I can’t edit the Participant role in Members, as its shown as uneditable.

    So I had assigned capability to subscriber role instead to get around this.

    But for keeping things sane, I would like to assign capability to Participant role, can you please suggest a solution? Which allows me to assign capability to participant Role.

    Note: I tried https://github.com/JJJ/bbp-capabilities, but I didn’t find a way to just add capability using it either, maybe I didn’t understood it 🙁

    Part #2:

    Also, is there any reliable solution, which allows forum users to easily upload media?
    As I have tried few of them (bbPress Multi Image Uploader, GD bbPress Attachments, Image Upload for BBPress etc), but none seem to allow for “Media Upload” capability instead they allow for attachments instead.

    Kind Regards,
    Syed

  2. Justin Tadlock

    Part 1

    bbPress roles are uneditable because they are dynamic and bbPress has explicitly declared them as uneditable. There’s nothing you can do with the Members plugin to change that.

    I did take a quick look in the bbPress plugin and saw this filter in includes/core/capabilities.php:

    return apply_filters( 'bbp_get_caps_for_role', $caps, $role );
    

    Assuming bbPress uses this filter across the entire plugin, you should be able to do this:

    add_filter( 'bbp_get_caps_for_role', 'th_bbp_caps_for_role', 10, 2 );
    
    function th_bbp_caps_for_role( $caps, $role ) {
    
        if ( bbp_get_participant_role() === $role ) {
    
            $caps['name_of_capability'] = true;
        }
    
        return $caps;
    }
    

    Just change name_of_capability to the cap you want to add.

    Part 2

    “Attachments” are the same thing as “Media” in WordPress parlance, so you might want to reconsider those existing plugins.

    That was going to be my suggestion anyway — to check if there were any plugins. There’s no easy way to upload media in a forum. You’d need a plugin specifically catered to doing that.

  3. Syed

    Part 1

    Thanks for the code,

    for some reasons, although it gets executed and add he capability, but doesn’t allow the power of capability in execution.

    But luckily, this code works,

    add_filter( 'user_has_cap', 'ct_user_has_cap', 10, 3 );
    
    function ct_user_has_cap( $allcaps, $cap, $args ) {
        # Get current user ID
        $user_id = get_current_user_id();
    
        # Get current user
        $current_user = get_user_by( 'id', $user_id );
    
        # Add capabilities
        if ( in_array( bbp_get_participant_role(), $current_user->roles ) ) {
    
            $allcaps['upload_files'] = true;
    
        }
    
        return $allcaps;
    }
    

    Should I be using this? Is it safe?

    Part 2,

    You are correct, but requirement is that user can add image to Text Editor, so can adjust it as per their post etc, not just attach it to post.

    But I am looking for a nice solution, not v. satisfied with the plugins I mentioned.

    V. disappointed to see that, after all these years there is not a reliable plugin for BBPresss, for this functionality, as I was hoping it will be a piece of cake, because of the age of BBPress and its community.

  4. Justin Tadlock

    Please see the formatting guide for how to post code in the forums. 🙂


    Filtering user_has_cap is OK. However, your code is checking the current user instead of the user related to the filter (not always the same). It should be:

    add_filter( 'user_has_cap', 'ct_user_has_cap', 10, 4 );
    
    function ct_user_has_cap( $allcaps, $caps, $args, $user ) {
    
        if ( in_array( bbp_get_participant_role(), (array)$user->roles ) ) {
    
            $allcaps['upload_files'] = true;
        }
    
        return $allcaps;
    }
    
  5. Syed

    I will try to format, from now on 🙂 Thanks.

    Yes, that’s true, that’s for catching the issue :), I didn’t thought it like that.