dongzhang6544 2019-04-28 16:34
浏览 76

如何为WordPress中已注销的用户添加重定向异常

I have the code below to redirect my WordPress logged out users to page /login/

<?php
if (!is_user_logged_in()) {
    if ( ! is_page( 'register-member' ) )
     if ( ! is_page( 'groups' ) ){
 wp_redirect( 'https://members.google.com/login/');
 exit;
} 
} ?>

And currently i whitelisted /register-member/ and /groups/ so logged out users can access them.

But I'm trying to whitelist more pages and it's not working and I think it's overwritten by the theme (BuddyPress)

Also i'm using "Restrict Content Pro" plugin

So i'm looking for a solution to whitelist other pages.

  • 写回答

1条回答 默认 最新

  • duanraa1984 2019-04-28 17:26
    关注

    You can do these things with is_user_logged_in() and is_front_page(). If these are not true, then you can use wp_redirect() to redirect the user to the home page.

    Assuming your forgot password page is a "page," you could use is_page() in your logic as well.

    You'll need to hook it to action so that it fires late enough to allow checking the page, but early enough to still safely redirect the user (i.e. before headers are sent). template_redirect is ideal for this.

    add_action( 'template_redirect', 'my_frontpage_redirect' );
    function my_frontpage_redirect() {
        if ( ! is_user_logged_in() ) {
            if ( ! is_front_page() && ! is_page( 'my-password-reset' ) ) {
                wp_redirect( home_url() );
                exit();
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题