skip to Main Content

I’m working on a WordPress application and we have added a custom field via the advanced custom fields plugin.

This is working well!

We need to set a specific role though based on the Registration Pin that we have added via the advanced custom fields plugin.

We tried to update the code in users.php, within the function wp_insert_user( $userdata ) { method, but we have failed.

Here is the code we tried to set:

debug_to_console('hey cindyyyyyyyyyyyyyy');
    debug_to_console($userdata);
    if ( isset( $userdata['role'] ) ) {
        $user->set_role( $userdata['role'] );
    } elseif ( ! $update ) {
        if ($userdata['registration_pin'] == 9742)
        {
            $user->set_role('club_member');
        }
        else if ($userdata['registration_pin'] == 9744) {
            $user->set_role('new_member');
        }
        else{
            $user->set_role( get_option( 'default_role' ) );
        }
        //$user->set_role( get_option( 'default_role' ) );
        //      $user->set_role('new_member');
        //  $user->set_role('club_member');
    }

In this code, it passes the 2 if statements and goes directly to the else which sets the ‘default role’.

2

Answers


  1. Chosen as BEST ANSWER

    Here is a valid answer:

        // validate the registration pin is 9999
        if ($field['_name'] === 'registration_pin')
        {
            if ($value !== "9999")
            {
                $message = "Registration pin is incorrect.";
                $valid   = false;
            }
        }
    

    Full code on function:

    function acf_validate_value( $value, $field, $input ) {
    
        // vars
        $valid   = true;
        $message = sprintf( __( '%s value is required', 'acf' ), $field['label'] );
    
        // valid
        if ( $field['required'] ) {
    
            // valid is set to false if the value is empty, but allow 0 as a valid value
            if ( empty( $value ) && ! is_numeric( $value ) ) {
    
                $valid = false;
    
            }
        }
    
        /**
        *  Filters whether the value is valid.
        *
        *  @date    28/09/13
        *  @since   5.0.0
        *
        *  @param   bool $valid The valid status. Return a string to display a custom error message.
        *  @param   mixed $value The value.
        *  @param   array $field The field array.
        *  @param   string $input The input element's name attribute.
        */
        $valid = apply_filters( "acf/validate_value/type={$field['type']}", $valid, $value, $field, $input );
        $valid = apply_filters( "acf/validate_value/name={$field['_name']}", $valid, $value, $field, $input );
        $valid = apply_filters( "acf/validate_value/key={$field['key']}", $valid, $value, $field, $input );
        $valid = apply_filters( 'acf/validate_value', $valid, $value, $field, $input );
    
        // validate the registration pin is 9999
        if ($field['_name'] === 'registration_pin')
        {
            if ($value !== "9999")
            {
                $message = "Registration pin is incorrect.";
                $valid   = false;
            }
        }
    
        // allow $valid to be a custom error message
        if ( ! empty( $valid ) && is_string( $valid ) ) {
    
            $message = $valid;
            $valid   = false;
    
        }
    
        if ( ! $valid ) {
    
            acf_add_validation_error( $input, $message );
            return false;
    
        }
    
        // return
        return true;
    
    }
    

  2. The only way it would pass those two "if statements" would be because either $userdata[‘registration_pin’] is not equal to 9742 or 9744, or $userdata[‘registration_pin’] doesn’t exist in the $userdata

    I am sure the problem is $userdata[‘registration_pin’] doesn’t exist, ACF wouldn’t save a custom field in your userdata like that… by default custom fields from ACF are saved in the user_meta table

    You should be able to access the registration key with this:

    $registration_key = get_user_meta($user_id, 'registration_key', true);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search