php 登录 WordPress 后重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8127453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:00:50  来源:igfitidea点击:

Redirect after Login on WordPress

phpwordpressredirectwordpress-theming

提问by Philip Kirkbride

I'm creating a customized WordPress theme based on an existing site.

我正在基于现有站点创建自定义的 WordPress 主题。

I want to use an alternate dashboard which I have created.

我想使用我创建的备用仪表板。

How can I have the user directed to 'news.php' after login instead of '/wp-admin/' ?

如何news.php在登录后将用户定向到 ' ' 而不是 ' /wp-admin/'?

--

——

EDIT: Have a working Plug-in for this but the bounty is still availible for anyone who can find a manual way to do this through functions.php, as it would be more secure then using a 3rd party plug-in.

编辑:为此有一个可用的插件,但任何可以通过functions.php找到手动方法的人仍然可以获得赏金,因为它比使用第3方插件更安全。

回答by Travis

This should solve your problem. Adapted from an answer found here.

这应该可以解决您的问题。改编自此处找到的答案

Add the following snippet of code in the functions.php file of your theme:

在主题的functions.php 文件中添加以下代码片段:

function admin_default_page() {
  return '/new-dashboard-url';
}

add_filter('login_redirect', 'admin_default_page');

回答by Dan

The accepted answer is very wrong. One should never be modifying the WordPress Core. Not only will edits be lost at a given update, some changes you make on a whim may compromise other functionality or even endanger the security of your site.

接受的答案是非常错误的。永远不应该修改 WordPress 核心。不仅会在给定更新时丢失编辑内容,而且您一时兴起所做的一些更改可能会损害其他功能,甚至危及您网站的安全。

Action Hooks & Filtersare included within the core to allow modifying functionality without modifying code.

核心中包含动作钩子和过滤器,允许在不修改代码的情况下修改功能。

An example of using the login_redirectfilter to redirect certain users can be found hereand is a muchmore robust solution to your problem.

使用的例子login_redirect过滤器重定向到特定的用户可以发现这里是一个更强大的解决您的问题。

For your specific problem, you want to do this:

对于您的特定问题,您希望这样做:

function login_redirect( $redirect_to, $request, $user ){
    return home_url('news.php');
}
add_filter( 'login_redirect', 'login_redirect', 10, 3 );

回答by Vasanthan.R.P

This may help. 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"...

登录和注销后将用户重定向到不同的位置。

为特定用户、具有特定角色的用户、具有特定功能的用户定义一组重定向规则,并为所有其他用户定义一揽子规则。此外,为注册后设置重定向 URL。这一切都在设置 > 登录/注销重定向中进行管理。

您可以[variable]username[/variable]在 URL 中使用语法,以便系统在每次登录时构建一个动态 URL,用用户的用户名替换该文本。除了用户名,还有“userslug”、“homeurl”、“siteurl”、“postid-23”、“http_referer”,你还可以添加自己的自定义URL“变量”...

回答by Leo Jiang

add_action('wp_head','redirect_admin');
function redirect_admin(){
  if(is_admin()){
    wp_redirect(WP_HOME.'/news.php');
    die; // You have to die here
  }
}

Or if you only want to redirect other users:

或者,如果您只想重定向其他用户:

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
  }
}

回答by gArn

The Theme My Loginplugin may help - it allows you to redirect users of specific roles to specific pages.

主题我的登录插件可以帮助-它可以让你特定角色的用户重定向到特定页面。

回答by Farahmand

If you have php 5.3+, you can use an anonymous function like so:

如果你有 php 5.3+,你可以像这样使用匿名函数:

add_filter( 'login_redirect', function() { return site_url('news'); } );

回答by Mohammad Altamash Ansari

Please try this, it works for any redirection on WordPress

请试试这个,它适用于 WordPress 上的任何重定向

add_filter('woocommerce_login_redirect', 'wc_login_redirect'); 

function wc_login_redirect( $redirect_to ) {

   $redirect_to = 'PUT HERE URL OF THE PAGE';
   return $redirect_to;

}

回答by Hossein

You can also use the customized link as:

您还可以将自定义链接用作:

https://example.com/wp-login.php?redirect_to=https://example.com/news.php

回答by Lokesh Metta

// add the code to your theme function.php
//for logout redirection
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
//for login redirection
add_action('wp_login','auto_redirect_after_login');
function auto_redirect_after_login(){
wp_redirect( home_url() );
exit();
`enter code here`}

回答by Mike

The accepted answer is clearly not a good answer! It may solve your problem for a while, but what will happen next time you update your WordPress installation? Your core files may get overridden and you will loose all your modifications.

接受的答案显然不是一个好的答案!它可能会暂时解决您的问题,但是下次更新 WordPress 安装时会发生什么?您的核心文件可能会被覆盖,您将丢失所有修改。

As already stated by others (Dan and Travis answers), the correct answer is to use the login_redirectfilter.

正如其他人(Dan 和 Travis 的回答)已经指出的那样,正确的答案是使用login_redirect过滤器。