从特定页面重定向已注销的用户


Redirecting logged out users from specific pages

我有一个wordpress网站,它使用buddypress和bbpress。我需要隐藏/重定向未登录的人的所有好友和 bbpress 页面。因此,如果有人登陆会员页面、个人资料页面或任何论坛主题,则需要将他们重定向到注册页面。

我尝试了大约 5 个插件,所有这些插件都导致了 404 错误、不起作用或只是白页等问题。

网址结构是这样的:

www.example.com/members
www.example.com/members/luke
www.example.com/forums
www.example.com/forums/forum/general-chat

有谁知道我如何在没有插件的情况下做到这一点?

您必须从子主题中修改配置文件循环.php文件

your-child-theme/members/single/profile/profile-loop.php

在文件的第一行,添加

<?php if ( is_user_logged_in()  ) : ?>

文件末尾,在最后一个 endif 和最后一个 endif 之间插入以下内容do_action:

<?php else : ?>
<?php echo “<div style=’width: 600px;height:25px; padding: 4px; border: 3px solid #ff0000; text-align: center; font-style:bold; font-size: 1.3em;’> You must be logged in to view a member profile</div>”; ?>
<?php endif; ?>

将div 内联样式更改为您需要的主题。该示例符合 bp 默认值。

如果你不能这样做,那么试试这个插件,.plugin

试试这个,但一定要把网址改成你想要的

add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page('add page slug or i.d here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/page/' ); 
    exit;
}

在你的theme/functions.php或 bp-custom.php 中试试这个:

function lukedi_private_check() {
    if ( ! is_admin() && ! is_user_logged_in() ) {
        if ( is_front_page() || is_home() || bp_is_register_page() || bp_is_activation_page() )
            return;
        $redirect_url = trailingslashit( site_url() );  // change this to whatever you need
        // member page
        if ( bp_is_user() )  
            bp_core_redirect( $redirect_url );
        // bbPress
        if( is_bbpress() ) 
            bp_core_redirect( $redirect_url );
        // members loop
        $bp_current_component = bp_current_component();
        if ( false != $bp_current_component ) {
            if ( 'members' == $bp_current_component )
                bp_core_redirect( $redirect_url );
        }
    }
}
add_action( 'bp_ready', 'lukedi_private_check' );