• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Store
    • Cart
    • Checkout
  • Blog
    • Basics [Free]
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Contact
  • Sign In
  • Show Search
Hide Search
Home » Tips and Tricks » Redirect a logged in user from a landing page

Redirect a logged in user from a landing page

Chad Butler · Aug 7, 2012 ·

Suppose you have a landing page you are directing users to, and users can login on that page, then be redirected to an articles page.  What happens if the user is already logged in and they hit the landing page?  Here is a process that will help smooth out the user experience. 

This article is only available to WP-Members Support Subscribers. If you have an existing subscription, please login below. If you do not have a current support subscription, you can purchase a support subscription here.

Already a Member? Log In Here
   
Forgot password? Click here to reset

To gain full access to WP-Members premium content, a current support subscription is required. You can purchase an annual support subscription for as little as $59, which provides you with access to priority support, a customer support forum, access to code snippets, and more.

Why wait? Choose your subscription option here.

[
Why join?]

Tips and Tricks is_page, is_user_logged_in, tips, wp_redirect

Welcome to RocketGeek Interactive › Forums › Redirect a logged in user from a landing page

Tagged: is_page, is_user_logged_in, tips, wp_redirect

  • This topic has 4 replies, 3 voices, and was last updated 6 years, 9 months ago by Chad Butler.
Viewing 3 reply threads
  • Author
    Posts
    • August 7, 2012 at 9:29 am #953
      Chad Butler
      Keymaster

      Suppose you have a landing page you are directing users to, and users can login on that page, then be redirected to an articles page.  What happens if
      [See the full post at: Redirect a logged in user from a landing page]

    • January 24, 2014 at 2:25 pm #3927
      toniM
      Participant

      This redirect is not working for me.
      I am trying to add a redirect once user has successfully logged in.
      Keeps staying on original Login page.

      Test login: ToniMills pswd: EIaGg9giEMdq

      Follows content of Child functions.php (I removed the opening php tag in case it messed up the form submit). What am I missing? Thanks
      —————–

      add_filter( ‘wpmem_login_form’, ‘my_login_form_filter’ );
      function my_login_form_filter( $form ) {
      return ”;
      }

      /**
      * Block menus based on login and user role using wp_nav_menu_args
      *
      * This is close to cut-and-paste-code. You will need to make
      * some changes to reflect the menu names and possibly role
      * capabilities. To use/test as-is, you’ll need to match up
      * menu names (value of $args[‘menu’] with the various (3) states:
      * * logged in, editor or higher role
      * * logged in, lower than editor role
      * * logged out
      */
      add_filter( ‘wp_nav_menu_args’, ‘wpmem_nav_menu’ );
      function wpmem_nav_menu( $args )
      {
      if( is_user_logged_in() ) {

      /** the user is logged in, display menu based on role */

      if( current_user_can( ‘edit_posts’ ) ) {

      // if the user has editor role or higher
      $args[‘menu’] = ‘editor-menu’; // editor menu name

      } else {

      // user is logged in, but lower than editor role
      $args[‘menu’] = ‘subscriber-menu’; // loer than editor menu

      }

      } else {

      /**
      * here the user is not logged in. display the
      * non-logged in menu
      */
      $args[‘menu’] = ‘logged-out’; // logged out menu name
      }

      return $args;
      }

      /**
      * WP-Members sidebar status filter example
      *
      * This is an example of the wpmem_sidebar_status
      * filter hook. The hook brings in the html $string
      * generated by the plugin and makes somes changes.
      */
      add_filter( ‘wpmem_sidebar_status’, ‘my_sidebar_status’ );
      function my_sidebar_status( $string )
      {
      /** get the user info in an object using get_currentuserinfo */
      global $current_user;
      get_currentuserinfo();

      /**
      * use str_replace to find the original “You are logged in as $user_login”
      * and replace it with “Welcome, Firstname Lastname” and make
      * it <b>bold</b>.  (NOTE: we are making the assumption here
      * that first_name/last_name are being used as required
      * registration fields
      */
      $replace = ‘<b>Welcome, ‘
      . $current_user->first_name . ‘ ‘
      . $current_user->last_name . ‘</b>’;
      $needle = ‘You are logged in as ‘ . $current_user->user_login;
      $string = str_replace( $needle, $replace, $string );

      /**
      * use str_replace to find the original “click here to logout”
      * and replace it with “Not Firstname?”
      */
      $replace = ‘Not ‘ . $current_user->first_name . ‘?’;
      $needle = ‘click here to logout’;
      $string = str_replace( $needle, $replace, $string );

      /**
      * find the user’s avatar, size it at 32px
      * attach it at the front of the html $string
      */
      $string = ‘<div style=”padding-top:4px;padding-right:4px;float:left;”>’
      . get_avatar( $current_user->ID, ’32’ )
      . ‘</div>’
      . $string;

      /** return the filtered html */
      return $string;
      }

      add_action( ‘wp_head’, ‘check_page’ );
      function check_page()
      {
      /**
      * If the page is the landing page AND the user is
      * logged in, then use wp_redirect to send the user
      * to some URL (presumeably the main site).
      */
      if( is_page( ‘landing-page’ ) && is_user_logged_in() ) {
      wp_redirect( ‘http://newportbayclub.com/login/nbc-documents/&#8217; );
      exit();
      }

      return;
      }

    • January 24, 2014 at 3:23 pm #3928
      Chad Butler
      Keymaster

      Hi Toni,

      It might be easier for me to take a direct look at this. Could you either (a) send me a copy of the functions.php file or (b) if the site is up in a location I can look at it, send me some login credentials?

      Contact me via the priority support contact form and I’ll email you back with info on where to send it.

    • May 16, 2014 at 1:50 pm #4887
      andrewblackwell
      Participant

      Hi Chad

      I tried to use this one (by pasting the code snippet into functions.php of the child theme and changing the links as per below), but got the following error message when trying to test it on the relevant page:

      Warning: Cannot modify header information – headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-content/themes/Divi/header.php:4) in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/pluggable.php on line 1121

      Is there a conflict with my theme?

      Here’s the code I use:

      //redirect "Medlemskab" when logged in
      
      add_action( 'wp_head', 'check_page' );
      function check_page()
      {
      	/**
      	 * If the page is the landing page AND the user is
      	 * logged in, then use wp_redirect to send the user
      	 * to some URL (presumeably the main site).
       	 */
      	if( is_page( 'medlemskab' ) && is_user_logged_in() ) {
      		wp_redirect( 'http://localhost/wordpress/medlem-velkomst' );
      		exit();
      	}
      
      	return;
      };

      Thanks!
      Andrew

      • May 19, 2014 at 10:08 pm #4911
        Chad Butler
        Keymaster

        Hi Andrew,

        wp_redirect needs to happen before any output is sent to the browser. The wp_head action is technically too late to do that (which is why you are getting the error that output is already started).

        You might hook this to the init action, but you may not be able to use is_page at that point.

  • Author
    Posts
Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Log In

Ready to get started?

Join Today!

© 2021 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Refund Policy