How to conditionally modify the user role on user creation

Created by Phil Kurth, Modified on Mon, 10 Oct 2022 at 07:51 AM by Phil Kurth

You may modify user data using the af/form/editing/user_data filter. To conditionally modify a user role based on a submitted value, use the af_get_field() function within the hooked callback as per the following example:


add_filter( 'af/form/editing/user_data', function ( $user_data, $form, $args ) {

  // If we aren't handling the desired form, return early.
  if ( $form['key'] !== 'FORM_KEY_HERE' ) {
    return $user_data;
  }

  // If a submitted value matches a condition, modify the user role.
  if ( af_get_field( 'some_submitted_field' ) === 'some value' ) {
    $user_data['role'] = 'author';
  }

  return $user_data;
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article