3

I use Ultimate Member and classic form for registration.

I have enabled "Strong password", but I want my users to at least fill their password with 1 special character (like "# % $ ...")

I didn't find any plugin, so I tried to find the good hook and I tried this code :

function um_submit_form_register_theme($args) {
    global $ultimatemember;
    $password = $_POST['user_password-257'];
    
    if ( !isset($ultimatemember->form->errors) ) {
        if (!preg_match("/\W/", $password)) {
            $ultimatemember->classes['form']->add_error( 'user_password-257', __('Your password must a special caractere') );
        } else {
            do_action('um_user_registration', $args); 
        }
    }
 
    do_action('um_user_registration_extra_hook', $args ); 
}
add_action('um_submit_form_register', 'um_submit_form_register_theme', 10);

But it doesn't work... The registration with no special character. Any clue ?

3
  • you can use javascript for this or use profile builder plugin Commented Sep 4, 2020 at 10:06
  • I did, I used the password strengh metter from Wordpress. But I would like to have a server check ... Commented Sep 4, 2020 at 13:24
  • please use this regex /^(?=.*[!@#$%^&*-])(?=.*[0-9])(?=.*[A-Z]).{8,20}$/ Commented Sep 4, 2020 at 13:33

1 Answer 1

0

You have to do various things. But you can change the hook the Ultimate Member uses. They Use the Validation class. But keeping your need in mind, you can bypass the use of this class. This is the solution I think will work

remove_action( 'um_submit_form_register', 'um_submit_form_register', 10 );

Now we will register new method.

add_action( 'um_submit_form_register', 'my_custom_submit_form_register', 10 );

Here is the function. What I think the developers must do in the next release is to provide filter inside the Validation class.

function my_custom_submit_form_register( $args ) {
    if ( isset( UM()->form()->errors ) ) {
        return false;
    }

    /**
     * UM hook
     *
     * @type filter
     * @title um_add_user_frontend_submitted
     * @description Extend user data on registration form submit
     * @input_vars
     * [{"var":"$submitted","type":"array","desc":"Registration data"}]
     * @change_log
     * ["Since: 2.0"]
     * @usage
     * <?php add_filter( 'um_add_user_frontend_submitted', 'function_name', 10, 1 ); ?>
     * @example
     * <?php
     * add_filter( 'um_add_user_frontend_submitted', 'my_add_user_frontend_submitted', 10, 1 );
     * function my_add_user_frontend_submitted( $submitted ) {
     *     // your code here
     *     return $submitted;
     * }
     * ?>
     */
    $args = apply_filters( 'um_add_user_frontend_submitted', $args );

    extract( $args );

    if ( ! empty( $username ) && empty( $user_login ) ) {
        $user_login = $username;
    }

    if ( ! empty( $first_name ) && ! empty( $last_name ) && empty( $user_login ) ) {

        if ( UM()->options()->get( 'permalink_base' ) == 'name' ) {
            $user_login = rawurlencode( strtolower( str_replace( " ", ".", $first_name . " " . $last_name ) ) );
        } elseif ( UM()->options()->get( 'permalink_base' ) == 'name_dash' ) {
            $user_login = rawurlencode( strtolower( str_replace( " ", "-", $first_name . " " . $last_name ) ) );
        } elseif ( UM()->options()->get( 'permalink_base' ) == 'name_plus' ) {
            $user_login = strtolower( str_replace( " ", "+", $first_name . " " . $last_name ) );
        } else {
            $user_login = strtolower( str_replace( " ", "", $first_name . " " . $last_name ) );
        }

        // if full name exists
        $count = 1;
        $temp_user_login = $user_login;
        while ( username_exists( $temp_user_login ) ) {
            $temp_user_login = $user_login . $count;
            $count++;
        }
        if ( $temp_user_login !== $user_login ) {
            $user_login = $temp_user_login;
        }
    }

    if ( empty( $user_login ) && ! empty( $user_email ) ) {
        $user_login = $user_email;
    }

    $unique_userID = UM()->query()->count_users() + 1;

    if ( empty( $user_login ) || strlen( $user_login ) > 30 && ! is_email( $user_login ) ) {
        $user_login = 'user' . $unique_userID;
    }

    if ( isset( $username ) && is_email( $username ) ) {
        $user_email = $username;
    }

    if ( ! isset( $user_password ) ) {
        $user_password = apply_filters('my_custom_password_strength', UM()->validation()->generate( 8 ));
    }

    if ( empty( $user_email ) ) {
        $site_url = @$_SERVER['SERVER_NAME'];
        $user_email = 'nobody' . $unique_userID . '@' . $site_url;
        /**
         * UM hook
         *
         * @type filter
         * @title um_user_register_submitted__email
         * @description Change user default email if it's empty on registration
         * @input_vars
         * [{"var":"$user_email","type":"string","desc":"Default email"}]
         * @change_log
         * ["Since: 2.0"]
         * @usage
         * <?php add_filter( 'um_user_register_submitted__email', 'function_name', 10, 1 ); ?>
         * @example
         * <?php
         * add_filter( 'um_user_register_submitted__email', 'my_user_register_submitted__email', 10, 1 );
         * function my_user_register_submitted__email( $user_email ) {
         *     // your code here
         *     return $user_email;
         * }
         * ?>
         */
        $user_email = apply_filters( 'um_user_register_submitted__email', $user_email );
    }

    $credentials = array(
        'user_login'    => $user_login,
        'user_password' => $user_password,
        'user_email'    => trim( $user_email ),
    );

    $args['submitted'] = array_merge( $args['submitted'], $credentials );
    $args = array_merge( $args, $credentials );

    //get user role from global or form's settings
    $user_role = UM()->form()->assigned_role( UM()->form()->form_id );

    //get user role from field Role dropdown or radio
    if ( isset( $args['role'] ) ) {
        global $wp_roles;
        $um_roles = get_option( 'um_roles' );

        if ( ! empty( $um_roles ) ) {
            $role_keys = array_map( function( $item ) {
                return 'um_' . $item;
            }, get_option( 'um_roles' ) );
        } else {
            $role_keys = array();
        }

        $exclude_roles = array_diff( array_keys( $wp_roles->roles ), array_merge( $role_keys, array( 'subscriber' ) ) );

        //if role is properly set it
        if ( ! in_array( $args['role'], $exclude_roles ) ) {
            $user_role = $args['role'];
        }
    }

    /**
     * UM hook
     *
     * @type filter
     * @title um_registration_user_role
     * @description Change user role on registration process
     * @input_vars
     * [{"var":"$role","type":"string","desc":"User role"},
     * {"var":"$submitted","type":"array","desc":"Registration data"}]
     * @change_log
     * ["Since: 2.0"]
     * @usage
     * <?php add_filter( 'um_registration_user_role', 'function_name', 10, 2 ); ?>
     * @example
     * <?php
     * add_filter( 'um_registration_user_role', 'my_registration_user_role', 10, 2 );
     * function my_user_register_submitted__email( $role, $submitted ) {
     *     // your code here
     *     return $role;
     * }
     * ?>
     */
    $user_role = apply_filters( 'um_registration_user_role', $user_role, $args );

    $userdata = array(
        'user_login'    => $user_login,
        'user_pass'     => $user_password,
        'user_email'    => $user_email,
        'role'          => $user_role,
    );

    $user_id = wp_insert_user( $userdata );

    /**
     * UM hook
     *
     * @type action
     * @title um_user_register
     * @description After complete UM user registration.
     * @input_vars
     * [{"var":"$user_id","type":"int","desc":"User ID"},
     * {"var":"$args","type":"array","desc":"Form data"}]
     * @change_log
     * ["Since: 2.0"]
     * @usage add_action( 'um_user_register', 'function_name', 10, 2 );
     * @example
     * <?php
     * add_action( 'um_user_register', 'my_user_register', 10, 2 );
     * function my_user_register( $user_id, $args ) {
     *     // your code here
     * }
     * ?>
     */
    do_action( 'um_user_register', $user_id, $args );

    return $user_id;
}

I didn't change anything just added filter, so no problem happens if the developers change anything(u will face problems if developers added new things to this otherwise I reached to them to add a filter). Here is the filtered function.

function my_custom_password_strength() {
    global $ultimatemember;
    $password = $_POST['user_password-257'];

     if (preg_match(" /^(?=.*[!@#$%^&*-])(?=.*[0-9])(?=.*[A-Z]).{8,20}$/", $password)) {
             return wp_generate_password(8)
        } else {
            $ultimatemember->classes['form']->add_error( 'user_password-257', __('Your password must have a special character') );
            do_action('um_user_registration', $args); 
        }
}

This is the only way you can do this. You may have to add a few more checks. But remember to just play with the filter function my_custom_password_strength().

Hope this helps. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.