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();
}
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.