PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

gform_username bug

  1. I've run in to a bit of a problem with gform_username. When I use the code provided on the documents:

    add_filter('gform_username', 'auto_username');
    function auto_username($username){
    
        $username = strtolower(rgpost('input_12_41_3') . rgpost('input_12_41_6'));
    
        if(empty($username))
            return $username;
    
        if(!function_exists('username_exists'))
            require_once(ABSPATH . WPINC . "/registration.php");
    
        if(username_exists($username)){
            $i = 2;
            while(username_exists($username . $i)){
                $i++;
            }
            $username = $username . $i;
        };
    
        return $username;
    }

    When I submit the form, the field I set as "username" from the user registration always returns a validation error stating "username must not be empty" (or something to that effect). This happened for any field I set as "username". If I changed the $_POST["input_42"] == 'some random string'; it still broke. Upon commenting the above code, the form worked perfectly fine. So it leads me to believe there's an issue with the function (I haven't dug in to the code).

    As a work-around I created a hidden field called uid which I set as the "username". I had to pre-populate this field via gform_field_value_uid so it was not empty, then use a gform_pre_submission to repopulate the uid $_POST value to what I wanted the username to actually be.

    add_action("gform_pre_submission", "populate_username");
    function populate_username($form){
        $_POST["input_42"] = $_POST["input_41_3"] . $_POST["input_41_6"];
        // input_41_3 & input_41_6 are first & last name based on advanced name field.
    }
    
    add_filter("gform_field_value_uid", "populate_uid");
    function populate_uid($value){
       return md5(rand_string(20));
    }
    Posted 12 years ago on Wednesday October 17, 2012 | Permalink
  2. It looks like you simply need to replace "input_12_41_3" with "input_41_3" and "input_12_41_6" with "input_12_41_6" from the original code you posted. Let me know if that doesn't do the trick.

    Posted 12 years ago on Wednesday October 17, 2012 | Permalink