dongyukang7006 2018-02-08 04:24
浏览 62
已采纳

Wordpress全局重定向登录到上一页

I am using Wordpress as an application that requires all users to be logged in to access. The function that I have been using to do this is

function logoutApp_redirect() {
global $post;
    if( ! is_user_logged_in() ){
        wp_redirect( home_url() . '/dashboard' );
    }
}
add_action( 'template_redirect', 'logoutApp_redirect' );

if a user is logged out then I want to return them to the page that they were previously on. For example, if a user is on home_url() . '/page1' ); and they are logged out for inactivity return them to this page upon login.

A few ideas I had was first to use AJAX and overlay the login (so they never leave the page they are on) and upon login the overlay is removed. This is my ultimate solution, yet for security, anyone with the knowledge of inspect could hide the overlay to see the private information.

Thus the reasoning for adding a redirect to the wp-login and bringing my second idea of when a user is logged out they are redirected and upon login they are returned to the previous page. This is the better method for security but it means that any information that was not stored to the DB will be lost.

How can I address a solution that will fix these issues?

PLEASE NOTE I do not want a plugin as the solution and would prefer to keep things operating from within my functions.php

  • 写回答

1条回答 默认 最新

  • doubo1871 2018-02-08 04:55
    关注

    You can use PHP $_SESSION

    if( ! is_user_logged_in() ){
        $_SESSION['user_previous_page'] = $server['REQUEST_URI'];
        wp_redirect( home_url() . '/dashboard' );
    }
    

    Then after the login you can send the user to the previous page

     wp_redirect(home_url() . $_SESSION['user_previous_page']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等