3

Is is possible to redirect a user on front end login to a post that they have created?

I have the form below on the front end which works great to log people in but doesn't quite achieve what I want.

I've created a front end registration process, that creates a new user and a post and fills in certain details into that post. On completion of that process they are sent to the post they just created, which then allows them to front end edit all the details. That works perfectly for first time registrants but I can't quite achieve what I want with people that have already registered and want to login.

When they fill in their details on the form below, I want it to redirect to the post they created when they registered, is this possible?

The users can only ever create ONE post... which is acting as a profile. So I want them to be able to log in and get redirected to their profile.

If it helps, the username is the same as the post title (the username is the persons company name, which is also the name of the post).

            <?php if (!(current_user_can('level_0'))){ ?>   
                <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post" id="login-form">
                    <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="<?php echo $_SERVER['REQUEST_URI']; ?>" />
                    <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 } ?>  

I'm guessing the following...

I need to do something with the username (entered in the form above) then match that with the author of any posts, if that matches then send them to that page... all before the submission of the form above. This needs to be done before the hidden field, so it can enter the username field into the hidden input value.

4 Answers 4

3

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;
    }
}
7
  • Ok thanks, I'll give this a go. I've nearly got it working with some horrible workarounds but I'd much prefer to do it your way!
    – Rob
    Feb 21, 2013 at 17:05
  • Just tried it out and it just redirects to the current page once it's logged them in. Doesn't look like it's linked up with the form.
    – Rob
    Feb 21, 2013 at 17:09
  • Just to be sure, did you add the above code to the template file or functions.php? I should have specified that it has to be in funcitons.php.
    – Jake
    Feb 21, 2013 at 17:14
  • You might want to try the more universal version that isn't locked into pages. I just assumed you were using a page, but I suppose there is a good chance you are using a custom_post_type.
    – Jake
    Feb 21, 2013 at 17:58
  • I gave both attempts a go but no luck. The item that is created during registration is a post, the login form is on a page. I've posted an answer that works but I'd much prefer to go in the direction that you and Warwick are suggesting.
    – Rob
    Feb 22, 2013 at 9:17
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;
    }
}
2
  • Yeah. good point. I just assumed it was a page.
    – Jake
    Feb 21, 2013 at 17:48
  • @Warwick The login form is on a page. A post gets created during the registration process. I've had a go at both yours and Jakes answer but no luck. I've also posted an answer - which currently works but I'd much prefer to do it with a hook/filter/action.
    – Rob
    Feb 22, 2013 at 9:15
2

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 } ?>
0

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.

Your Answer

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

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