問題描述
我正在根據現有網站建立一個定製的 WordPress 主題。
我想使用我建立的備用儀錶板。
登入後如何將使用者定向到 「news.php」,而不是 「/wp-admin/」?
–
編輯:為此工作有一個工作的 Plug-in,但是對於任何可以透過 functions.php 找到手動方式的人仍然可以使用賞金,因為使用第三方 plug-in 將更安全。
最佳解決思路
這應該可以解決你的問題。改編自 here. 的答案
function admin_default_page() {
return '/new-dashboard-url';
}
add_filter('login_redirect', 'admin_default_page');
次佳解決思路
接受的答案是非常錯誤的。不應該修改 WordPress 核心。不僅會在給定的更新中丟失編輯內容,而且您所做的一些更改可能會損害其他功能,甚至危及您網站的安全。
Action Hooks & Filters 包含在核心中,以允許修改功能而不修改程式碼。
使用 login_redirect 過濾器重定向某些使用者的示例可以找到 here,並且是您的問題更加強大的解決方案。
對於您的具體問題,您要這樣做:
function login_redirect( $redirect_to, $request, $user ){
return home_url('news.php');
}
add_filter( 'login_redirect', 'login_redirect', 10, 3 );
第三種解決思路
這可能有幫助。 Peter’s Login Redirect
Redirect users to different locations after logging in and logging out.
Define a set of redirect rules for specific users, users with specific roles, users with specific capabilities, and a blanket rule for all other users. Also, set a redirect URL for post-registration. This is all managed in Settings > Login/logout redirects.
You can use the syntax
[variable]username[/variable]in your URLs so that the system will build a dynamic URL upon each login, replacing that text with the user’s username. In addition to username, there is “userslug”, “homeurl”, “siteurl”, “postid-23”, “http_referer” and you can also add your own custom URL “variables”…
第四種思路
add_action('wp_head','redirect_admin');
function redirect_admin(){
if(is_admin()){
wp_redirect(WP_HOME.'/news.php');
die; // You have to die here
}
}
或者如果您只想重定向其他使用者:
add_action('wp_head','redirect_admin');
function redirect_admin(){
if(is_admin()&&!current_user_can('level_10')){
wp_redirect(WP_HOME.'/news.php');
die; // You have to die here
}
}
第五種思路
Theme My Login 外掛可能有幫助 – 它允許您將特定角色的使用者重定向到特定頁面。
第六種思路
如果您有 php 5.3+,您可以使用如下所示的匿名函式:
add_filter( 'login_redirect', function() { return site_url('news'); } );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。