Useful redirects

//******************************************************************
//******************************************************************
//********** REDIRECT WP LOGIN PAGE TO OUR SITE HOME PAGE **********
//******************************************************************
//******************************************************************
//Fires page : /wp-login.php
add_action( 'login_head', 'myplugin_add_login_fields' );
function myplugin_add_login_fields() 
{
  if (strpos($_SERVER["REQUEST_URI"], '/wp-admin/') === False)
  {
    wp_redirect( home_url( '/' ) );
    die();
  }
}

//*************************************************
//*************************************************
//********** TEMPLATE PAGE ABOUT TO LOAD **********
//*************************************************
//*************************************************
//Hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the
//content that has been queried.
add_action("template_redirect", 'mysite_template_redirect');
function mysite_template_redirect()
{

  //----- LOGIN PAGE RE-DIRECTS TO USER PROFILE ONCE LOGGED IN -----
  if ( (is_user_logged_in()) && ($_SERVER["REQUEST_URI"] == '/') )
  {
    $Username = bp_core_get_username(get_current_user_id());
    wp_redirect( home_url( "/members/$Username/") );
    die();
  }
  
  //----- ONLY ALLOW LOGGED IN USERS TO VIEW BUDDYPRESS CONTENT -----
  if (
    (!is_user_logged_in()) &&
    ( (strpos($_SERVER["REQUEST_URI"], '/members') !== False) || (strpos($_SERVER["REQUEST_URI"], '/groups') !== False) )
  )
  {
    wp_redirect( home_url( '/' ) );
    die();
  }
  
  //----- MENU LOGOUT LINK -----
  if ($_SERVER["REQUEST_URI"] == '/logout')     //<< So you can use '/logout' as your menu logout link 
  {
    wp_logout();
    wp_redirect( home_url( '/' ) );
    die();
  }

}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *