Please or Register to create posts and topics.

Automaticly change user role when creating first topic

Hi,

How can I automaticly change the user role from Subscriber to another role when the user create his first topic in the Asgaros forum?

Tnx!

Hey @fanste,

you could use this code snippet to change the user role:

function change_user_role($post_id, $topic_id, $subject, $content, $link, $author_id) {
    // Fetch the WP_User object of the topic author
    $user = new WP_User( $author_id );

    // Change user role to editor if role is currently subscriber
    if($user->has_cap('subscriber')){
        // Change 'editor' to the role that you want to give your user aftert submitting a topic
        $user->set_role( 'editor' );
    }
}

add_action('asgarosforum_after_add_topic_submit', 'change_user_role', 10, 6);

 

Asgaros and fanSte have reacted to this post.
AsgarosfanSte
If you ever wonder how to return the favour for helping you.

Tnx, that worked. But I forgot to ask how that can I combine the new role with another forum usergroup.

So, if a user post his first topic in the forum, the user role changed, but also his forum usergroup from one to another.

You can change the forum user role by using this function:

AsgarosForumUserGroups::insertUserGroupsOfUsers($author_id, $assigned_groups = array('your_user_group'));

 

Asgaros has reacted to this post.
Asgaros
If you ever wonder how to return the favour for helping you.

tnx, but I need a little bit of help with that. How can I combine this function with the snippet you’ve posted earlier?

So when a user posted his first topic, his Wordpress user role automaticly change from “a” to “b” and his forum user group automaticly change from “aa” to “bb”.

Hey @fanste,

just inert the second snippet inside  the first code I’ve sent  you:

function change_user_role($post_id, $topic_id, $subject, $content, $link, $author_id) {
    // Fetch the WP_User object of the topic author
    $user = new WP_User( $author_id );

    // Change user role to editor if role is currently subscriber
    if($user->has_cap('subscriber')){
        // Change 'editor' to the role that you want to give your user aftert submitting a topic
        $user->set_role( 'editor' );
    }
    
    // Add forum user group to user
    AsgarosForumUserGroups::insertUserGroupsOfUsers($author_id, $assigned_groups = array('your_user_group'));
}

add_action('asgarosforum_after_add_topic_submit', 'change_user_role', 10, 6);

So every user that creates a topic will be  added to the ‘you_user_group’ forum user group and if the user is a ‘subscriber’ he will become an ‘editor’.

Asgaros has reacted to this post.
Asgaros
If you ever wonder how to return the favour for helping you.

That works, thank you!

Asgaros and qualmy91 have reacted to this post.
Asgarosqualmy91