我想用 BuddyPress 创建一个个人资料目录,但我希望网站在搜索网站上的任何个人资料时要求用户登录/注册。在描述中,如果有人想根据性别、位置或技能搜索任何个人资料,用户应该登录以查看所有搜索到的个人资料。 我试过下面的代码,但它不工作。
function bp_guest_redirect() {
global $bp;
if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_page( BP_MEMBERS_SLUG ) ) {
// enter the slug or component conditional here
if(!is_user_logged_in()) { // not logged in user
wp_redirect( home_url("/register/" );
} // user will be redirect to any link to want
}
}
add_filter('get_header','bp_guest_redirect',1);
请建议我应该如何在搜索后将用户重定向到登录/注册页面。
此代码仅限制对成员目录搜索功能的访问,并使用
bp_before_directory_members_content
动作挂钩检查用户是否未登录并使用wp_login_url
功能将他们重定向到登录页面。
function bp_guest_redirect() {
if ( bp_is_members_component() && ! is_user_logged_in() ) {
wp_redirect( wp_login_url() );
exit();
}
}
add_action( 'bp_before_directory_members_content', 'bp_guest_redirect' );