1

I am using WordPress Multisite with Paid Membership Pro for two types of member users (Corporate companies, individuals). I have subsites for the corporate users (ex. corporate.mysite.com) that has member access to content on mysite.com. I have setup different 'Dashboards' for individual tiers and corporate users. For instance a 'rookie' user is redirected to mysite.com/rookie-dashboard/ and a corporate user is redirected to corporate.mysite.com/dashboard/ after logged in. Each member is setup as a subscriber and is assigned a user level ID based on which package they purchased (via PaidMembershipPro)

That said, I'm trying to setup a function switch redirect if a user tries to access the wrong dashboard. Here is what I have setup so far:

 function dashboard_redirect()
{
    global $current_user, $post;


        switch ($current_user->membership_level->id) {
        case "2":
        // Rookie Member level
        wp_redirect('/rookie-dashboard/');
        break;
        case "3":
        // Player Member level
        wp_redirect('/player-dashboard/');
        break;
        case "4":
        // All-Star Member level
        wp_redirect('/allstar-dashboard/');
        break;
        case "5":
        // Coach Member level
        wp_redirect('/coach-dashboard/');
        break;
        case "6":
        // Owner Member level
        if (! is_page(197) ) { 
            return false; 
        } else {
     wp_redirect('/dashboard/'); 
}
        break;
        case "7":
                if (! is_page('/dashboard/') ) { 
            return false; 
        } else {
        // Corporate
        wp_redirect('http://corporate.mysite.com/dashboard/');
                }
        }   

}

add_action('template_redirect', 'dashboard_redirect');

The issue that I am running into is nulling the process when the right user is on their dashboard page. I get infinite loops if I just have a switch casse with the redirects so I tried adding a if/then to the switch but can't seem to get it to work. Not sure what I'm doing wrong.

0

The problem is that template_redirect fires on every public page load. That means that after you redirect a user to a specific page, template_redirect fires again, even if they're already on the correct page. You just need to add a check to see if the current page they are on is same as where they should be.

function dashboard_redirect() {
  global $current_user, $post;

  //* Escape early if not on a 'dashboard' page
  $current = trailingslashit( $_SERVER[ 'REQUEST_URI' ] );
  if( false === stripos( $current, 'dashboard' ) ) {
    return;
  }

  switch ( $current_user->membership_level->id ) {
    case "2":
      // Rookie Member level
      $redirect = '/rookie-dashboard/';
      break;
    case "3":
      // Player Member level
      $redirect = '/player-dashboard/';
      break;
    //* Etc.
  }

  //* Redirect if we're not already on the right page
  $current = trailingslashit( $_SERVER[ 'REQUEST_URI' ] );
  if( $current !== $redirect ) {
    wp_redirect( $redirect );
  }
}

//* Conditionally add action when user tries to access a 'dashboard' page
$current = trailingslashit( $_SERVER[ 'REQUEST_URI' ] );
if( false !== stripos( $current, 'dashboard' ) ) {
  add_action( 'template_redirect', 'dashboard_redirect' );
}

Added example code on how you can conditionally add the hook OR how to escape early from the callback if we're not on a dashboard page. Use one or the other. I'd probably use the escape method, but only because it's cleaner.

  • Thanks Nathan! How can I make it so that the template redirect is only triggered on the Dashboard Pages? For instance I have the 5 member level dashboard pages on the main site and the corporate dashboard on the subsite. Right now, the way it's setup I am unable to go to any other page on the site. – Danny Feb 27 '17 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.