简体   繁体   English

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

[英]Redirecting logged out users from specific pages

I have a wordpress website, its using buddypress and bbpress. 我有一个wordpress网站,使用的是buddypress和bbpress。 I need to hide/redirect all the buddypress and bbpress pages from people who are not logged in. So if someone lands on the members page, profile page or any forum topic it needs to redirect them to the signup page. 我需要从未登录的人员隐藏/重定向所有的buddypress和bbpress页面。因此,如果某人登陆到成员页面,个人资料页面或任何论坛主题,则需要将其重定向到注册页面。

I tried maybe 5 plugins, all of them caused issues like 404 errors, not working or just white pages. 我尝试了5个插件,它们全部都引起了诸如404错误之类的问题,无法正常工作或仅仅是白页。

The url structure is like this: 网址结构如下:

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

Does anyone know how I can do this without a plugin? 有谁知道我没有插件怎么办?

you have to modify from within a child theme the profile-loop.php file 您必须在子主题中修改profile-loop.php文件

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

On the first line of the file, add 在文件的第一行,添加

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

At the end of the file, insert between the last endif and the last do_action this: 在文件末尾,在最后一个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; ?>

Change the div inline style to whatever you need accordingly to your theme. 将div内联样式更改为主题所需的任何内容。 The example fits with bp-default. 该示例适合bp-default。

If you can not do that, then try this plugin, plugin 如果您不能这样做,请尝试使用该插件

Try this but make sure to change the url to what you want 尝试此操作,但请确保将URL更改为所需的URL

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;
}

Try this in your theme/functions.php or in bp-custom.php : 在您的theme/functions.phpbp-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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM