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.
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.
Either way, you can use this action to update the login redirect, add it to functions.php:
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:
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.
This currently works but it feels a bit dirty! If there’s a better way then I’d rather go with that.
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.