• Resolved Jim

    (@jwmc)


    I want to avoid users ever going to the wp-admin/profile.php page, which seems to be the WordPress default after login. I have a shortcode block that, for unlogged users, tracks their current URL (on pages with the shortcode) and redirects them back to that page after login. It works great:

    
    function fp_logged_in() {
    	if ( is_user_logged_in() ) {
    		$current_user = wp_get_current_user();
    		$displayname = $current_user->display_name;
    		$text = 'Logged in as <b>' . $displayname . '</b><br \><a href="' . home_url() . '/edit-profile/">Edit or Delete Profile</a><br \><a href="' . wp_logout_url() . ' ">Log Out</a>';
    	} else {
    		global $wp;
    		$current_url = home_url( add_query_arg( array(), $wp->request ) );		
    		$URL = esc_url( wp_login_url( $current_url ) );
    		$text = '<p style="font-size:x-small;">Registration is needed only to post in the fora and blog.</p><a href="' . $URL . '">Login</a><br \><a href="' . wp_registration_url() . ' ">Register</a>';
    	}
    	return $text;
    }
    add_shortcode( 'fp_user_stuff', 'fp_logged_in' );

    The problem is new users. After going through the register/confirm/password/login process, since they didn’t go through the shortcode, they still get sent to the backend. I tried this to redirect Subscribers to the home page, but for some reason, it causes authentication failure when logging in

    
    add_action('admin_init', 'fp_redirect');
    function fp_redirect() {
        if( !current_user_can('edit_posts') ) 
            wp_redirect(home_url());
    }

    How can I keep the page-specific redirect in the shortcode, but still direct new users to the home page after their registration/login process?

    • This topic was modified 3 years, 1 month ago by Jim.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Try using this plugin rather than your hooks: https://www.ads-software.com/plugins/peters-login-redirect/

    Thread Starter Jim

    (@jwmc)

    Thanks. It looks like I would have to get the PRO version just to do the page-specific redirects I do with my shortcode. Beyond that, I’d like to avoid all the unneeded code that comes with a plugin.

    Hey Jim,

    A similar question was asked on the forum, and it seems that this code achieves what you are looking for:

    function admin_default_page() {
      return 'https://www.yoursite.com.com?post_type=artigo';
    }
    
    add_filter('login_redirect', 'admin_default_page');

    Have a good day

    Thread Starter Jim

    (@jwmc)

    Thanks, but that’s just a plain old redirect. It overrides everything. But this works, keeping the redirect requested in my shortcode if present, otherwise redirecting to the home page.

    function fp_login_redirect( $redirect_to, $request, $user ) {
        	if ($request == '')
        		return home_url();
        	else
        		return $request;
    }
    add_filter( 'login_redirect', 'fp_login_redirect', 10, 3 );
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘redirects after logging in’ is closed to new replies.