简体   繁体   English

在 WordPress 上登录后重定向

[英]Redirect after Login on WordPress

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方插件更安全。

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');

The accepted answer is very wrong.接受的答案是非常错误的。 One should never be modifying the WordPress Core.永远不应该修改 WordPress 核心。 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.不仅会在给定更新时丢失编辑内容,而且您一时兴起所做的一些更改可能会损害其他功能,甚至危及您网站的安全。

Action Hooks & Filters are included within the core to allow modifying functionality without modifying code.核心中包含动作钩子和过滤器,允许在不修改代码的情况下修改功能。

An example of using the login_redirect filter to redirect certain users can be found here and is a much more 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 );

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.此外,为注册后设置重定向 URL。 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.您可以在 URL 中使用语法[variable]username[/variable] ,以便系统在每次登录时构建一个动态 URL,用用户的用户名替换该文本。 In addition to username, there is "userslug", "homeurl", "siteurl", "postid-23", "http_referer" and you can also add your own custom URL "variables"...除了用户名,还有“userslug”、“homeurl”、“siteurl”、“postid-23”、“http_referer”,你还可以添加自己的自定义URL“变量”...

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

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

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

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

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

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?它可能会暂时解决您的问题,但是下次更新 WordPress 安装时会发生什么? Your core files may get overridden and you will loose all your modifications.您的核心文件可能会被覆盖,您将丢失所有修改。

As already stated by others (Dan and Travis answers), the correct answer is to use the login_redirect filter.正如其他人已经说过的(Dan 和 Travis 的回答),正确的答案是使用login_redirect过滤器。

Theme My Login插件可能会有所帮助 - 它允许您将特定角色的用户重定向到特定页面。

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;

}

Here is a code snippet to redirect user based on user role:这是根据用户角色重定向用户的代码片段:

add_filter( 'login_redirect', 'kp_login_redirect', 10, 3 );

function kp_login_redirect( $redirect_to, $request, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'subscriber', $user->roles )) {
             $redirect_to = home_url("dashboard");
        }
    }
    return $redirect_to;
}

If you are using Network site (multisite) then try this:如果您使用的是网络站点(多站点),请尝试以下操作:

add_filter( 'login_redirect', 'kp_login_redirect', 10, 3 );

function kp_login_redirect( $redirect_to, $request, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'subscriber', $user->roles )) {
             $redirect_to = network_site_url("dashboard");
        }
    }
    return $redirect_to;
}
// Used theme's functions.php  
add_action('login_form', 'redirect_after_login'); 
function redirect_after_login() 
{     
global $redirect_to; 
if   (!isset($_GET['redirect_to'])) 
{ 
$redirect_to =   get_option('sample-page');
//  sample-page = your page name after site_url
} }
// 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`}

Based on some of the other answers, I came up with this:根据其他一些答案,我想出了这个:

/**
 * Require login on all pages
 */
function so8127453_require_login() {
  if ( ! is_user_logged_in() ) {

    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
    $base = $_SERVER['SERVER_NAME'];
    $uri = $_SERVER['REQUEST_URI'];

    $attempted_accessed_url = $protocol . $base . $uri;
    $login_url = 'https://' . $base . '/wp-login.php?redirect_to=' . $attempted_accessed_url;

    wp_redirect( $login_url );
  }
}
add_action( 'template_redirect', 'so8127453_require_login' );

Redirects all traffic to a login-page.将所有流量重定向到登录页面。 And then to the attempted access URL afterwards.然后到之后尝试访问的 URL。

I was searching "How to Fix WordPress Login Page Refreshing and Redirecting Issue?"我在搜索“如何修复 WordPress 登录页面刷新和重定向问题?” and did not find any good fix.并没有找到任何好的修复方法。 From this Stackoverflow question, I have got my help.从这个 Stackoverflow 问题中,我得到了帮助。 I would like to share it with others so that in case they need it, they get the help.我想与其他人分享,以便他们在需要时获得帮助。

On my website, when I was entering email and password, I was redirecting again and again to wp-admin and asked for passwords.在我的网站上,当我输入电子邮件和密码时,我一次又一次地重定向到 wp-admin 并要求输入密码。 This code helped me to fix the issue:这段代码帮助我解决了这个问题:

function admin_default_page() {
  return '/';
}

add_filter('login_redirect', 'admin_default_page');

for WooCommerce: redirects for login / logout - working in 2021 WooCommerce:登录/注销重定向 - 2021 年工作

add_filter('woocommerce_login_redirect', 'login_redirect');

function login_redirect($redirect_to) {

    return home_url();

}

add_action('wp_logout','logout_redirect');

function logout_redirect(){

    wp_redirect( home_url() );
    
    exit;

}

Right out of the box I found this which does it for me.开箱即用,我发现这对我有用。 You can just replace the argument with a hardcoded site if you like如果你愿意,你可以用硬编码的站点替换参数

wp_loginout($_SERVER['REQUEST_URI']); 

This one is also good plugin for this requirment.这也是这个要求的好插件。

https:\/\/wordpress.org\/plugins\/role-based-redirect\/<\/a> https:\/\/wordpress.org\/plugins\/role-based-redirect\/<\/a>

"

There are various ways to do that, will leave two options below:有多种方法可以做到这一点,将在下面留下两个选项:

  1. Using wp_redirect :使用wp_redirect

If OP knows the ID of the page, the following will do the work如果 OP 知道页面的 ID,以下将完成工作

function add_login_check()
{
    if ( is_user_logged_in() && is_page(765)) {
        wp_redirect('https://sample.com/dash'); # Using wp_redirect
        exit;
    }
}
  1. Using wp_safe_redirect使用wp_safe_redirect

This requires one to know the ID of a specific page.这需要知道特定页面的 ID。

function add_login_check()
{
    if (is_user_logged_in())) {
        if (is_page(765)){ # If the user is in the page with the ID 765
            wp_safe_redirect( get_permalink('234')); # To get the URL of a specific page from the ID, and using wp_redirect
            exit; 
        }
    }
}

To globally redirect after successful login, find this code in wp-login.php, under section.要在成功登录后全局重定向,请在 wp-login.php 部分下找到此代码。

   <form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">

<input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />

and replace <?php echo esc_attr($redirect_to); ?>并替换<?php echo esc_attr($redirect_to); ?> <?php echo esc_attr($redirect_to); ?> with your URL where you want to redirect. <?php echo esc_attr($redirect_to); ?>使用您要重定向的 URL。 The URL must start with http:// and ends on /other wise page redirect to default location. URL 必须以 http:// 开头并以 /other wise 页面重定向到默认位置结束。

Do same thing form redirect after registration with in same file but under <form name="registerform"> section.使用相同的文件但在<form name="registerform">部分下注册后做同样的事情表单重定向。

The functions.php file doesn't have anything to do with login redirect, what you should be considering it's the wp-login.php file, you can actually change the entire login interface from there, and force users to redirect to your custom pages instead of the /wp-admin/ directory. functions.php 文件与登录重定向没有任何关系,您应该考虑的是 wp-login.php 文件,您实际上可以从那里更改整个登录界面,并强制用户重定向到您的自定义页面而不是 /wp-admin/ 目录。

Open the file with Notepad if using Windows or any text editor, Prese Ctrl + F (on window) Find "wp-admin/" and change it to the folder you want it to redirect to after login, still on the same file Press Ctrl + F, find "admin_url" and the change the file name, the default file name there is "profile.php"...after just save and give a try.如果使用 Windows 或任何文本编辑器,请使用记事本打开文件,按 Ctrl + F(在窗口上)找到“wp-admin/”并将其更改为登录后要重定向到的文件夹,仍在同一文件上按 Ctrl + F,找到“admin_url”并更改文件名,默认文件名是“profile.php”...保存后试一试。

if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) )
        $redirect_to = admin_url('profile.php');
    wp_safe_redirect($redirect_to);
    exit();

Or you can use the "registration-login plugin" http://wordpress.org/extend/plugins/registration-login/ , just simple edit the redirect urls and the links to where you want it to redirect after login, and you've got your very own custom profile.或者您可以使用“注册登录插件” http://wordpress.org/extend/plugins/registration-login/ ,只需简单地编辑重定向 url 和登录后您希望它重定向到的链接,您就可以有您自己的自定义配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM