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.

Form Direct to Private Page with Auto Login

  1. studio57designs
    Member

    I've read all the posts regarding this topic and I've not found a solution or at least a solution that was easy for me to implement.

    I have a form that once submitted will redirect to a private page with links on that page to download a free item.

    I want the user to fill in the form, be redirected to the second page and automatically logged in with a specific username and password, so they can see the private page.

    I've tried the following code in my theme functions.php file.

    add_action( 'gform_user_registered', 'pi_gravity_registration_autologin', 10, 4 );
    /**
     * Auto login after registration.
     */
    function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
    	$user = get_userdata( $user_id );
    	$user_login = $user->user_login;
    	$user_password = $password;
    
        wp_signon( array(
    		'user_login' => 'freeitem',
    		'user_password' =>  'samplepassword',
    		'remember' => false
        ) );
    }

    But, I don't really understand how I customize the above code to get it right and even more specific, just for the one form, not all forms.

    Any ideas would be greatly appreciated! Love Gravity Forms!!

    Louise

    Posted 7 years ago on Thursday November 15, 2012 | Permalink
  2. What happened when you tried that code? Did you customize it at all, or just try that as is?

    To make this apply to just one form, you can do something like this between existing lines 5 and 6:

    [php]
    if(117 == $entry['form_id]( {

    And then close that conditional between lines 14 and 15. That would make the code run only for entries submitted via form 117. Change that form ID to your own form ID.

    To debug your sign on function, I would assign the object returned by wp_signon to a variable, then check that for errors. Something like this (change line 10 and then add to it):

    [php]
    $user = wp_signon( array(
    		'user_login' => 'freeitem',
    		'user_password' =>  'samplepassword',
    		'remember' => false
        ) );
    // see what $user contains, as a debugging step
    var_dump($user);
    // or, check for an error
    if ( is_wp_error($user) )
       echo $user->get_error_message();
    Posted 7 years ago on Saturday November 17, 2012 | Permalink
  3. studio57designs
    Member

    Chris,

    I tried what you suggested:

    function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
    	if(5 == $entry['form_id']) {
    	$user = get_userdata( $user_id );
    	$user_login = $user->user_login;
    	$user_password = $password;
    
    	$user = wp_signon( array(
    		'user_login' => 'freeitem',
    		'user_password' =>  'samplepassword',
    		'remember' => false
        ) );
    // see what $user contains, as a debugging step
    var_dump($user);
    // or, check for an error
    if ( is_wp_error($user) )
       echo $user->get_error_message();
    
        }
    }

    All that was returned, was the 404 error page not found. When I'm already logged in, I am directed to the private page.

    I added the debug code, but I'm not really sure where I should be seeing this.

    I appreciate any help.

    Louise

    Posted 7 years ago on Monday November 26, 2012 | Permalink
  4. When do you get a 404 error page?

    The debug output should be shown on screen; sometimes in the source of the page, but normally right in the browser. It's normally a big mess.

    Posted 7 years ago on Tuesday November 27, 2012 | Permalink
  5. studio57designs
    Member

    Chris, I get a 404 page when I'm not logged in already and submit the form.
    I did not see any debugging code anywhere. Is this something if I send you the URL, you would look at?

    Louise

    Posted 7 years ago on Friday November 30, 2012 | Permalink
  6. Yes, I can look at the form online if you like. Please send the URL to chris@rocketgenius.com and reference this topic. If you think I need it, please also send a WordPress administrator login.

    Posted 7 years ago on Saturday December 1, 2012 | Permalink
  7. Louise, are you using the Gravity Forms User Registration Add-on to register your users? I did not see it installed on your site. The gform_user_registered action will only be available if you are using the User Registration Add-on.

    Posted 7 years ago on Tuesday December 11, 2012 | Permalink
  8. One problem I found when debugging this: make sure the role you are assigning to your registered users has sufficient capabilities to see private posts or pages. There are two specific capabilities built in to WordPress:

    read_private_pages
    read_private_posts

    If you are registering users and you want to log them in and have them see a private page or post, be sure that role you are assigning them has permission to view protected content.

    Posted 7 years ago on Tuesday December 11, 2012 | Permalink
  9. Finally, I was able to use this code to log the user in after registering with the User Registration add-on:

    [php]
    add_action('gform_user_registered', 'gf_autologin', 10, 4);
    function gf_autologin($user_id, $config, $entry, $password) {
        wp_set_auth_cookie($user_id, false, '');
    }

    http://pastebin.com/HNTWhPSr

    Posted 7 years ago on Tuesday December 11, 2012 | Permalink
  10. studio57designs
    Member

    Chris, absolutely the best! I was able to get the form to work as I needed and I also added a new user role in the functions.php, just for this registration form.

    I also added a simple plugin, so that once a user was logged in they could not see or change the password. Making it possible to keep them from logging back in later or giving their login details to a friend.

    Works great.

    Thanks so much - Happy Holidays.

    Louise

    Posted 7 years ago on Tuesday December 11, 2012 | Permalink
  11. Thanks for the update Louise.

    Posted 7 years ago on Monday December 17, 2012 | Permalink

This topic has been resolved and has been closed to new replies.