Leave a Reply

4 comments

  1. If I were you, I would use the wp_login_form function to create your form, but it looks like you got everything right, and maybe you have a good reason to use a manual form.

    <?php wp_login_form(); ?>
    

    Either way, you can use this action to update the login redirect, add it to functions.php:

    add_action( 'login_redirect', 'custom_redirect_login', 10, 3 );
    
    function custom_redirect_login( $redirect_to, $request, $user )
    {
        $posts = get_pages( array(
            'authors' => $user->ID
        ) );
        if (! empty($posts) )
        {
            // Since the pages are listed in DESC order, the first one is the most
            // recently created.
            return get_permalink($posts[0]->ID);
        }
        else
        {
            // If no posts associated with the user, use default.
            return $redirect_to;
        }
    }
    

    As noted, if you need to redirect to a post or custom_post_type instead of a page, you will need to use a non-page-specific method, so this might be better since it will work more universally:

    function custom_redirect_login( $redirect_to, $request, $user )
    {
        $posts = new WP_Query( array( 'author' => $user->ID ) );
        if ($posts->have_posts())
        {
            // Since the pages are listed in DESC order, the first one is the most
            // recently created.
            return get_permalink($posts->posts[0]->ID);
        }
        else
        {
            // If no posts associated with the user, use default.
            return $redirect_to;
        }
    }
    
  2. The item that gets created and stores the information, is it a “page” or a “post”.

    If it is a “post” get_pages in the filter in the previous answer wouldnt work (it will only get items with a post type of “page”). Try this filter out.

    function custom_redirect_login( $redirect_to, $request, $user )
    {
        $posts = new WP_Query( 'author='.$user->ID );
        if ($posts->have_posts())
        {
            // Since the pages are listed in DESC order, the first one is the most
            // recently created.
    
            return get_permalink($posts->posts[0]->ID);
        }
        else
        {
            // If no posts associated with the user, use default.
            return $redirect_to;
        }
    }
    
  3. This currently works but it feels a bit dirty! If there’s a better way then I’d rather go with that.

    <script>
    function onSubmit(){
        var str = document.forms["login"]["log"].value;
        var str = str.replace(/s+/g, '-').toLowerCase();
        document.forms["login"]["redirect_to"].value = str;
    }
    </script>                   
    
                        <?php if (!(current_user_can('level_0'))){ ?>   
                            <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post" name="login" onsubmit="onSubmit()">
                                <p style="color:black!IMPORTANT;">Please login.</p>
                                <!--[if !(IE)]><!-->
                                    <input type="text" placeholder="Username" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" />
                                <!--<![endif]-->
                                <!--[if (gte IE 6)]>
                                    <input type="text" name="log" id="log" value="Username" />
                                <![endif]-->
                                <!--[if !(IE)]><!-->
                                    <input type="password" placeholder="Password" name="pwd" id="pwd" />
                                <!--<![endif]-->
                                <!--[if (gte IE 6)]>
                                    <input type="password" value="Password" name="pwd" id="pwd" />
                                <![endif]-->    
                                <p style="clear:both;width:115px;font-size:14px!IMPORTANT;float:left;">
                                    <input style="width:14%!IMPORTANT;" name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me
                                </p>
    
    
                                <input type="hidden" name="redirect_to" value="" />
                                <p style="float:right;">
                                    <a style="font-size:14px!IMPORTANT;" href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a>
                                </p>                                
                                <input style="margin-left:80px;float:left;" type="submit" name="submit" value="LOGIN" class="login-button" />                               
                            </form>
                            <div class="clear"></div>
                        <?php } else { ?>
                            <p>
                                You are currently logged in, would you like to <a href="<?php echo wp_logout_url($_SERVER['REQUEST_URI']); ?>">logout</a>?
                            </p>
                        <?php } ?>
    
  4. maybe you can find a answer in my Question here: How to get user-meta from Social Login registered users?

    I use Gravityforms to have users create only one page from front-end (using the page.php as template), and directs them to their newly created page after submission. (all inbuilt Gravityforms settings, except the function to create a page, but you can find it in my link)

    But when users come back, I want them to show a welcome message with a link to their page instead of the form (hence they only complete the form once)

    It works fine for users registered by admin via admin dashboard. But it does not work for users registered via social login plugins. (that is what my question is about)

    So, part of the question behind my link could be your answer, but if you like users to register via social login, you’ll want to find the answer on my question too. 😉

    I hope it helps.