Skip to content

Instantly share code, notes, and snippets.

@moafzalmulla
Last active May 21, 2016 13:41
Show Gist options
  • Select an option

  • Save moafzalmulla/5ce3413c8055b8d28c526390f1b1afc4 to your computer and use it in GitHub Desktop.

Select an option

Save moafzalmulla/5ce3413c8055b8d28c526390f1b1afc4 to your computer and use it in GitHub Desktop.

Revisions

  1. moafzalmulla revised this gist May 21, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Gravity forms login form
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@


    // You must set the css classes of both input fields to username and password respectively for this to work
    // Code forums - cant remember exactly link
    // the _3 prefix has to match the id of the form you have created
    add_action( "gform_after_submission_20", "login_form_after_submission_20", 10, 2 );
    function login_form_after_submission_20($entry, $form) {
  2. moafzalmulla created this gist May 21, 2016.
    42 changes: 42 additions & 0 deletions Gravity forms login form
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@


    // the _3 prefix has to match the id of the form you have created
    add_action( "gform_after_submission_20", "login_form_after_submission_20", 10, 2 );
    function login_form_after_submission_20($entry, $form) {
    // get the username and pass
    $username = $entry[1];
    $pass = $entry[2];
    $creds = array();
    // create the credentials array
    $creds['user_login'] = $username;
    $creds['user_password'] = $pass;
    // sign in the user and set him as the logged in user
    $sign = wp_signon( $creds );
    wp_set_current_user( $sign->ID );
    }


    // the _3 prefix has to match the id of the form you have created
    add_filter( "gform_field_validation_20", "login_validate_field_20", 10, 4 );
    function login_validate_field_20($result, $value, $form, $field) {
    // make sure this variable is global
    // this function is fired via recurrence for each field, s
    global $user;
    // validate username
    if ( $field['cssClass'] === 'username' ) {
    $user = get_user_by( 'login', $value );
    if ( empty( $user->user_login ) ) {
    $result["is_valid"] = false;
    $result["message"] = "Invalid username provided.";
    }
    }
    // validate pass
    if ( $field['cssClass'] === 'password' ) {
    if ( !$user or !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) {
    $result["is_valid"] = false;
    $result["message"] = "Invalid password provided.";
    }
    }
    return $result;
    }