簡體   English   中英

在 WordPress 上登錄后重定向

[英]Redirect after Login on WordPress

我正在基於現有站點創建自定義 WordPress 主題。

我想使用我創建的備用儀表板。

如何在登錄后將用戶定向到“ news.php ”而不是“ /wp-admin/ ”?

--

編輯:為此有一個工作插件,但任何可以通過functions.php找到手動方法的人仍然可以獲得賞金,因為它比使用第3方插件更安全。

這應該可以解決您的問題。 改編自此處找到的答案

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

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

add_filter('login_redirect', 'admin_default_page');

接受的答案是非常錯誤的。 永遠不應該修改 WordPress 核心。 不僅會在給定更新時丟失編輯內容,而且您一時興起所做的一些更改可能會損害其他功能,甚至危及您網站的安全。

核心中包含動作鈎子和過濾器,允許在不修改代碼的情況下修改功能。

使用的例子login_redirect過濾器重定向到特定的用戶可以發現這里是一個強大的解決方案,您的問題。

對於您的特定問題,您希望這樣做:

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

這可能會有所幫助。 彼得的登錄重定向

登錄和注銷后將用戶重定向到不同的位置。

為特定用戶、具有特定角色的用戶、具有特定功能的用戶定義一組重定向規則,並為所有其他用戶定義一攬子規則。 此外,為注冊后設置重定向 URL。 這一切都在設置 > 登錄/注銷重定向中進行管理。

您可以在 URL 中使用語法[variable]username[/variable] ,以便系統在每次登錄時構建一個動態 URL,用用戶的用戶名替換該文本。 除了用戶名,還有“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
  }
}

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

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

接受的答案顯然不是一個好的答案! 它可能會暫時解決您的問題,但是下次更新 WordPress 安裝時會發生什么? 您的核心文件可能會被覆蓋,您將丟失所有修改。

正如其他人已經說過的(Dan 和 Travis 的回答),正確的答案是使用login_redirect過濾器。

Theme My Login插件可能會有所幫助 - 它允許您將特定角色的用戶重定向到特定頁面。

請試試這個,它適用於 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;

}

這是根據用戶角色重定向用戶的代碼片段:

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

如果您使用的是網絡站點(多站點),請嘗試以下操作:

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

根據其他一些答案,我想出了這個:

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

將所有流量重定向到登錄頁面。 然后到之后嘗試訪問的 URL。

我在搜索“如何修復 WordPress 登錄頁面刷新和重定向問題?” 並沒有找到任何好的修復方法。 從這個 Stackoverflow 問題中,我得到了幫助。 我想與其他人分享,以便他們在需要時獲得幫助。

在我的網站上,當我輸入電子郵件和密碼時,我一次又一次地重定向到 wp-admin 並要求輸入密碼。 這段代碼幫助我解決了這個問題:

function admin_default_page() {
  return '/';
}

add_filter('login_redirect', 'admin_default_page');

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;

}

開箱即用,我發現這對我有用。 如果你願意,你可以用硬編碼的站點替換參數

wp_loginout($_SERVER['REQUEST_URI']); 

有多種方法可以做到這一點,將在下面留下兩個選項:

  1. 使用wp_redirect

如果 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. 使用wp_safe_redirect

這需要知道特定頁面的 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; 
        }
    }
}

要在成功登錄后全局重定向,請在 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); ?>" />

並替換<?php echo esc_attr($redirect_to); ?> <?php echo esc_attr($redirect_to); ?>使用您要重定向的 URL。 URL 必須以 http:// 開頭並以 /other wise 頁面重定向到默認位置結束。

使用相同的文件但在<form name="registerform">部分下注冊后做同樣的事情表單重定向。

functions.php 文件與登錄重定向沒有任何關系,您應該考慮的是 wp-login.php 文件,您實際上可以從那里更改整個登錄界面,並強制用戶重定向到您的自定義頁面而不是 /wp-admin/ 目錄。

如果使用 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();

或者您可以使用“注冊登錄插件” http://wordpress.org/extend/plugins/registration-login/ ,只需簡單地編輯重定向 url 和登錄后您希望它重定向到的鏈接,您就可以有您自己的自定義配置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM