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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制