X

User Controlled Optional WordPress Login Redirect

Snippets by IsItWP

Do you want to add a dropdown menu on the login screen for your users to select the page they want to redirect after login? While there’s probably a plugin for this, we have created a quick code snippet that you can use to user controlled option WordPress login redirect.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

<?php
// Fields for redirect
function custom_login_fields() {
?>
	<p>
		<label>
			<strong>Choose your location: </strong>
			<select name="login_location">
				<option value="">Select &hellip;</option>
				<option value="<?php bloginfo('url'); ?>#banking">Banking</option>
				<option value="<?php bloginfo('url'); ?>#insurance">Insurance</option>
				<option value="<?php echo get_permalink(2); ?>">Securities</option>
			</select>
		</label>
	</p><br/>
<?php
}
 
// Redirect function
function location_redirect() {
	$location = $_POST['login_location'];
	wp_safe_redirect($location);
	exit();
}
 
// Add fields to the login form
add_action('login_form','custom_login_fields');
 
// Make sure the redirect happens only if your fields are submitted
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) 
	add_filter('login_redirect', 'location_redirect', 10, 3);
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress business themes for your website and 15 best content marketing tools and plugins for WordPress.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]