function tmg_force_login_redirect() {
if ( ! defined( 'DOING_AJAX' ) ) {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( is_page( 'account/dashboard' ) && is_user_logged_in() && 'tmg' === $role_name ) {
wp_redirect( 'https://virginiainstitute.org/account/login/', 301 );
exit;
}
}
add_action( 'wp_head', 'tmg_force_login_redirect' );
function tmg_force_login_redirect( $redirect_to, $user) {
global $user;
if ( isset( $user->roles ) && is_array ( $user->roles ) ) {
if( in_array( 'tmg', $user->roles ) && is_page( '/account/dashboard' ) ) {
return $redirect_to;
} else {
return wp_redirect( 'https://virginiainstitute.org/account/login');
exit;
}
} else {
return $redirect_to;
}
/* if ( ! defined( 'DOING_AJAX' ) ) {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( is_page( '/account/dashboard' ) && is_user_logged_in() && 'tmg' === !$role_name ) {
wp_redirect( 'https://virginiainstitute.org/account/login/', 301 );
exit;
}
}*/
}
add_action( 'login_redirect', 'tmg_force_login_redirect' );
function cm_redirect_users_by_role() {
if ( ! defined( 'DOING_AJAX' ) ) {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name ) {
wp_redirect( 'http://yoursite.com/dashboard' );
} // if $role_name
} // if DOING_AJAX
} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );
add_filter( 'login_redirect', 'login_redirect_shop_manager_on_orders_list', 10, 3 );
function login_redirect_shop_manager_on_orders_list( $redirect_to, $request, $user ) {
$defined_user_role = 'shop_manager'; // The defined user role
if( isset($user->roles) && is_array($user->roles) && in_array( $defined_user_role, $user->roles ) ) {
$redirect_to = admin_url('edit.php?post_type=shop_order'); // Custom redirection url
wp_safe_redirect( $redirect_to ); // Force redirection
exit(); // Mandatory to avoid errors
}
return $redirect_to;
}
// This PHP code redirects any user who isn’t capable of updating the core (Only admins) to a specific url on your site.
add_action( 'admin_init', 'redirect_non_admins_to_specific_page' );
function redirect_non_admins_to_specific_page() {
if ( ! current_user_can( 'update_core' ) && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/shop/' );
exit;
}
}
// OR
add_filter('login_redirect', 'wpsites_login_redirect', 10, 3);
function wpsites_login_redirect( $redirect_to, $request, $user ){
global $user;
if( isset( $user->roles ) && is_array( $user->roles ) ) {
if( in_array( '!administrator', $user->roles ) ) {
return $redirect_to;
} else {
return wp_redirect( 'http://www.example.com/' );
exit;
}
}
else {
return $redirect_to;
}
}
add_action( 'template_redirect', 'redirect_to_blog_page' );
function redirect_to_blog_page() {
if ( is_page('shop') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/blog/', 301 );
exit;
}
}
add_action( 'template_redirect', 'redirect_to_home_page' );
function redirect_to_home_page() {
if ( is_page('shop') && ! is_user_logged_in() ) {
wp_redirect( home_url(), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirect_to_external_url' );
function redirect_to_external_url() {
if ( is_page( 'old' ) ) {
wp_redirect( 'http://www.example.com/', 301 );
exit;
}
}
// Redirect Registration Page
function my_registration_page_redirect()
{
global $pagenow;
if ( ( strtolower($pagenow) == 'wp-login.php') && ( strtolower( $_GET['action']) == 'register' ) ) {
wp_redirect( home_url('/registration-url'));
}
}
add_filter( 'init', 'my_registration_page_redirect' );
// Redirect Registration Page
add_filter('register','hjs_register_url');
function hjs_register_url($link){
/* Change wp registration url */
return str_replace(site_url('wp-login.php?action=register', 'login'),site_url('register', 'login'),$link);
}
add_filter('site_url', 'hjs_fix_register_urls', 10, 3);
function hjs_fix_register_urls($url, $path, $orig_scheme){
if ($orig_scheme !== 'login')
return $url;
if ($path == 'wp-login.php?action=register')
return site_url('register', 'login');
return $url;
}
/*
Plugin Name: Nice Login URL
Plugin URI: en.bainternet.info
Description: Simple plugin to redirect login/register to a nice url
Version: 1.0
Author: bainternet
Author URI: en.bainternet.info
*/
// Add rewrite rule and flush on plugin activation
register_activation_hook( __FILE__, 'NLURL_activate' );
function NLURL_activate() {
if (! get_option('permalink_structure') ){
add_action('admin_notices', 'permalink_structure_admin_notice');
}
NLURL_rewrite();
flush_rewrite_rules();
}
// Flush on plugin deactivation
register_deactivation_hook( __FILE__, 'NLURL_deactivate' );
function NLURL_deactivate() {
flush_rewrite_rules();
}
// Create new rewrite rule
add_action( 'init', 'NLURL_rewrite' );
function NLURL_rewrite() {
add_rewrite_rule( 'login/?$', 'wp-login.php', 'top' );
add_rewrite_rule( 'register/?$', 'wp-login.php?action=register', 'top' );
add_rewrite_rule( 'forgot/?$', 'wp-login.php?action=lostpassword', 'top' );
}
//register url fix
add_filter('register','fix_register_url');
function fix_register_url($link){
return str_replace(site_url('wp-login.php?action=register', 'login'),site_url('register', 'login'),$link);
}
//login url fix
add_filter('login_url','fix_login_url');
function fix_login_url($link){
return str_replace(site_url('wp-login.php', 'login'),site_url('login', 'login'),$link);
}
//forgot password url fix
add_filter('lostpassword_url','fix_lostpass_url');
function fix_lostpass_url($link){
return str_replace('?action=lostpassword','',str_replace(network_site_url('wp-login.php', 'login'),site_url('forgot', 'login'),$link));
}
//Site URL hack to overwrite register url
add_filter('site_url','fix_urls',10,3);
function fix_urls($url, $path, $orig_scheme){
if ($orig_scheme !== 'login')
return $url;
if ($path == 'wp-login.php?action=register')
return site_url('register', 'login');
return $url;
}
//notice if user needs to enable permalinks
function permalink_structure_admin_notice(){
echo 'Please Make sure to enable Permalinks.
';
}