ds42774 2014-02-19 15:26
浏览 75
已采纳

如何自动注销用户并将其重定向到WordPress中带有自定义消息的登录页面

When any page is viewed, I need to make sure that the user ID is in a custom database table, and if the id is not present there, I want to log the user out and not allow him to see any content. I also want to display a custom message on the login page saying "You are not authorised to see this blog."

I tried doing this with some of the wp logout/redirect functions, but I want this to be automatic on all pages across the wp site. Also I don't know how to sent the custom message to the login page. I guess I could just set a POST value or something like that and modify the login page to check if that value is present.

Any help or tips on how to do this properly would be appreciated.

  • 写回答

1条回答 默认 最新

  • drtpbx3606 2014-02-19 15:48
    关注

    Try following

    Put the code below in your theme templates.

        //Logic for checking whether the ID exists or not
        if(empty($id)){
           wp_logout();
        }
    

    Put following in your function.php

        //When the user is logged out redirect him/her to the login page with a custom parameter
        add_action('wp_logout','go_home');
        function go_home(){
          wp_redirect( '/wp-login.php?action=logout&custom-logout=yes' );
          exit();
        }
    
        //If the custom parameter is set display the message on the login screen
        if(!empty($_GET['custom-logout']) && strtolower($_GET['custom-logout']) == "yes"){
         function custom_login_message() {
             $message = '<p class="message">Add your message here...</p><br />';
             return $message;
         }
         add_filter('login_message', 'custom_login_message');
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭