dsjhejw3232 2014-01-04 02:42
浏览 10
已采纳

已登录用户的条件页面(仅限会员页面)

I want to make my wordpress site hidden or restricted from guest user. Only the the registered member will see my pages, if they are not registered then they will be redirected in login page/ registration page.

When i am tried to use some plugin to do that then all of my pages restricted and there had option to exclude only one page (login page). But i guest cant register there. Because all of the page restricted excluding loging page. SO thats a problem.

Now i think i can use custom code something like that

<?php if ( is_user_logged_in() ) { echo 'Welcome, registered user!';} else { echo 'You have no rights to access this page. Please <a href=\"#\">log in</a> or a href=\"#\">register</a> now to access that page'; } ?>

But how can i set a function , "If logged in then full page will open other wise show a text"

  • 写回答

1条回答 默认 最新

  • dt102282 2014-01-04 02:51
    关注

    If you want to hide the content, then you can use the_content hook to change the page content for not loggedin user.

    add_filter('the_content', 'restricted_content');
    function restricted_content($content)
    {
        if( !is_user_logged_in()
        {
            $content = 'You have no rights to access this page...blah blah..';
        }
        return $content;
    }
    

    Now, if the login/registration is a General WordPress Page created using WP Admin, you can either exclude it by id, name or slug.

    function restricted_content($content)
    {
        if( !is_user_logged_in() && ! is_page('login') && ! is_page('registration') // an id or name can be passed also
        {
            $content = 'You have no rights to access this page...blah blah..';
        }
        return $content;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题