【问题标题】:How to redirect on previous to previous page in wordpress如何在wordpress中重定向上一页
【发布时间】:2013-08-06 11:06:21
【问题描述】:

我是wordpress 的新手。我目前正在注册和登录。我有一个页面限制网址让http://xyz.com/abc。此网址需要登录/注册。

我需要当用户注册或登录时,它应该被重定向到这个先前的 url。

在登录时我添加了以下钩子:

一个

add_filter('login_redirect', 'redirect_previous_page', 10);

function redirect_previous_page(){

    global $user;
    $request = $_SERVER["HTTP_REFERER"];

    if ( in_array( $user->roles[0], array( 'administrator') ) ) {

        return admin_url();
        return $redirect_to;

    } elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {

        return $request;
        return $redirect_to;

    } 

}

它对我有用,但我不知道如何在成功注册后重定向到此 url。我在登录页面上有注册链接

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
        add_filter('login_redirect', 'my_login_redirect', 10, 3);
        function my_login_redirect() {
            $location = $_SERVER['HTTP_REFERER'];
            wp_safe_redirect($location);
            exit();
        }
    }
    

    【讨论】:

      【解决方案2】:

      docs 中所述,您可以使用:<?php wp_get_referer() ?> 获取用户在注销前或登录后访问的最后一个 URL。

      【讨论】:

      • 是的 - 如果有一个“wp_”的方式,那就用它吧!
      【解决方案3】:

      这是一种粗略的方法,但是您可以创建 $_SESSION['your_registration_referrer'] 并粘贴您的 $_SERVER["HTTP_REFERER"] 并在注册后重定向到此网址。

      【讨论】:

      • 我正在使用theme-my-login插件,那么我应该把代码放在哪里阅读呢?
      【解决方案4】:

      我需要在用户注册或登录时将其重定向到之前的网址。

      如果您不需要对某些页面进行永久重定向,您可以在wp_login_url() 中使用重定向

      // current page with get parameters
      $redirect_after_login = home_url( add_query_arg( null, null ) ); 
      // or just
      $redirect_after_login = get_the_permalink();
      $link_to_login = wp_login_url( $redirect_after_login );
      

      您可以在wp_safe_redirect() 中使用此链接,或者只是在某处回显

      这是登录后重定向的更正代码:

      add_filter( 'login_redirect', 'redirect_previous_page', 10, 1 );
      function redirect_previous_page( $redirect_to ) {
          // don't change url if current user is admin or if user inside admin page
          if ( is_admin() || current_user_can( 'administrator' ) ) {
              return $redirect_to;
          }
          // also, we can check is current user has some role
          // if (current_user_can( 'subscriber' ))
          
          // change redirect url to previus page for non-administrators
          $redirect_to= wp_get_referer();
      
          // referer can return 'false' so add fallback
          return $redirect_to ? $redirect_to : home_url();
      }
      

      附:不幸的是,没有人写过两次“回归”。第二个“返回”不发送,因为无法实现

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 2018-11-16
        • 2022-06-18
        • 2021-02-12
        相关资源
        最近更新 更多