1. 程式人生 > >WordPress用戶登錄後重定向到指定頁面

WordPress用戶登錄後重定向到指定頁面

rac .com auto ble admin strong eight 定義 key

這篇文章將向您展示WordPress用戶登錄後如何重定向到指定頁面或者文章的技巧。

技術分享

一、重定向到網站管理面板。

將以下代碼添加到您的當前主題的 functions.php 文件中:

  1. function soi_login_redirect($redirect_to, $request, $user)
  2. {
  3. return (is_array($user->roles) && in_array(‘administrator‘, $user->roles)) ? admin_url() : site_url();
  4. }
  5. add_filter(‘login_redirect‘, ‘soi_login_redirect‘, 10, 3);

二、重定向到指定頁面或者文章

您還可以將用戶重定向到特定頁面,代碼如下:

  1. function login_redirect( $redirect_to, $request, $user ){
  2. return home_url(‘custom-page‘);
  3. }
  4. add_filter( ‘login_redirect‘, ‘login_redirect‘, 10, 3 );

修改代碼中的custom-page為某個頁面或者文章的鏈接(註:不包括站點域名),如果刪除custom-page會直接跳轉到站點首頁。


如何重定向WordPress登出/登陸後的鏈接

  今天的WordPress教程對於一個WordPress開發者來說,自定義用戶登出或者登陸後的鏈接是非常有用的。默認情況下WordPress會將登出之後的鏈接重定向到你的默認登陸頁面,但是我們可以很輕易的改變它,通過增加如下代碼到你的主題的functions.php中:

/*  
*自定義登出之後的重定向鏈接    
*/  
add_action(‘wp_logout‘,‘auto_redirect_after_logout‘);   
function auto_redirect_after_logout(){   
  wp_redirect( home_url() );   
  exit();   
}

  這裏我們將用戶登陸後的鏈接自動重定向也就是跳轉到我們網站的主頁,當然你也可以將home_url()替換成其他你希望的網址。如:

wp_redirect( ‘http://www.wpmee.com‘ );

  上面的WordPress教程中,我們講述了,登出之後的重定向鏈接,那麽下面我們將告訴大家如何自定義登陸之後的鏈接呢?這次並不是簡單的跳轉到首頁了,而是先判斷是否是管理員,如果是管理員就重定向到管理員面板,當然,如果是普通用戶則跳轉到首頁。將下面的代碼片段添加到你主題的functions.php中:

/*  
*自定義登陸之後的重定向鏈接   
*/  
function soi_login_redirect($redirect_to, $request, $user)   
{   
    return (is_array($user->roles) && in_array(‘administrator‘, $user->roles)) ? admin_url() : site_url();   
}    
add_filter(‘login_redirect‘, ‘soi_login_redirect‘, 10, 3); 

  當然你也可以將所有的登陸用戶跳轉到一個特別的頁面:

/*  
*自定義登陸之後的重定向到特殊的頁面   
*/  
function login_redirect( $redirect_to, $request, $user ){   
    return home_url(‘custom-page‘);   
}   
add_filter( ‘login_redirect‘, ‘login_redirect‘, 10, 3 );

  將其中的custom-page換成你需要跳轉的頁面鏈接即可,非常簡單的wordpress教程,希望對你有用。

WordPress用戶登錄後重定向到指定頁面