Support

Account

Home Forums General Issues Change user role on a front-end form save?

Helping

Change user role on a front-end form save?

  • What I need is:

    There is a front-end form created with acf_form that populates user fields (for example user website, date of birth etc…)

    What I need is that when a currently logged in user submits a form from front-end and the data is then saved to user profile (I can manage this without issue with acf_form and correct location rules), that user role changes from subscriber to some other role I’ll create.

    Basically once the user submits the form he will immediately get another user role.

    Any ideas?

    Thanks

  • Hi @dzonivejin,

    Thanks for the post.

    You can hook into the acf/pre_save_post filter and then change the user roles using the WP_User class. A user’s role can be set by creating an instance of the WP_user class, and calling the add_role() or remove_role() methods.

    The code would look like so:

    
    $u = new WP_User( $user_id );
    
    // Remove role
    $u->remove_role( 'subscriber' );
    
    // Add role
    $u->add_role( 'editor' );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Change user role on a front-end form save?’ is closed to new replies.