Skip to:
Content
Pages
Categories
Search
Top
Bottom

Change user role based on user post count?


  • Palagrin
    Participant

    @palagrin

    Is it possible to change user roles based on user post counts? Ie. if a user reaches 50 posts, BBP automatically changes their user role?

    I can’t seem to find much but I imagine it would be possible?

Viewing 6 replies - 1 through 6 (of 6 total)

  • Palagrin
    Participant

    @palagrin

    This seems to be a good starting point.

    Ok, so whilst there doesn’t seem to be a function I can filter that based on the post count, I could potentially run a function that changes someone’s role based on their post count every time they login? That would keep database queries to a minimum and still ensure the update happens fairly regularly.

    Any idea what filter or action I can use when a user logs in? Can I just use the wp_login action?


    Palagrin
    Participant

    @palagrin

    Potentially something like this, although it isn’t working as far as I can tell:

    function bbp_change_user_role_login($user) {
    
    $user_id = get_current_user_id($user);
    $current_user_role = bbp_get_user_role($user_id);
    $new_role_forum_role="bbp_moderator";
    $user_post_count = bbp_get_user_post_count( $user_id );
    
    if (($current_user_role = "bbp_participant") and ($user_post_count > 20)) {
    bbp_set_user_role( $user_id, $new_forum_role );
    }
    
    }
    add_action('wp_login', 'bbp_change_user_role_login');
    

    Robin W
    Moderator

    @robin-w

    I’d suggest you crack open this plugin which does topic and reply counts

    bbp topic count


    Robin W
    Moderator

    @robin-w

    you can also check out this plugin which does user ranking

    bbp user ranking


    Palagrin
    Participant

    @palagrin

    Thanks. Do you know how I can loop through all users in a function? I can’t seem to work it out.


    Robin W
    Moderator

    @robin-w

    $users= get_users () ;
    if ( $users ) :
    foreach ( $users as $user ) :
    $user_data = get_userdata( $user->ID );
    //whatever you want here				
    endforeach ;					
    endif ;
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar