Home / admin / User controlled optional wordpress login redirect
  • User controlled optional wordpress login redirect

    user-controlled-optional-login-redirect

    Do you require a more complex way to redirect users around your website when they login. This is a great method to use if you want to give your subscribers the ability to choose the page they are redirected to. You could provide visitors the option to redirect directly to their profile page or a custom settings page for your wordpress site. Adding this snippet to the functions.php of your wordpress theme will add a select menu to the login screen that will allow your users to choose where they would like to be redirected.

    If redirecting based on role or capabilities is more your style take a look at this snippet.

    ( example screenshot )

    wordpress snippet : PHP

    <>
    <?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);
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    login_redirect, bloginfo, get_permalink, add_filter, add_action, wp, wp_safe_redirect,

Build a better with wpsnipp.com

Weekly snippets, themes, deals coupons roundup.

x No Thanks