Redirecting logged out users from specific pages - php

I have a wordpress website, its using buddypress and 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.
I tried maybe 5 plugins, all of them caused issues like 404 errors, not working or just white pages.
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
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:
<?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. The example fits with 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
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:
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' );

Related

WordPress restricted page redirect for non logged users to login page with PHP

I would like to redirect a visitor of a WordPress website to a login page when they are not logged in and on a certain page. I have the following PHP, which doesn't seem to work. Does anyone know what I am doing wrong?
add_action( 'admin_init', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( '3220' ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}
template_redirect is the right hook to handle redirection.
Here is updated code:
add_action( 'template_redirect', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( 3220 ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}

How to add redirection exception for logged out users in 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.
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();
}
}
}

PHP to redirect 1 post is applying on all my post

I would like no redirect not logged in user who is trying to access a specific product, to My Account page to help them register (in WooCommerce).
I'm using the code below, but at present all the products redirect to My account:
function reg_redirect(){
if( is_product(1735) && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
An idea what's wrong ?
That's because is_product() doesn't accept a post ID as an argument like is_single() and similar functions do.
You could simply use get_the_ID(), though:
function reg_redirect(){
if( 1735 == get_the_ID() && !is_user_logged_in() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
Note that you should also use get_permalink() and get_option() together to get the URL of your account page, rather than 'hard-coding' it (passing it as a string).
I am unfamiliar with woocommerce, but according to https://docs.woocommerce.com/wc-apidocs/function-is_product.html, I don't think is_product is the function you want. Try this.
global $product;
function reg_redirect(){
$id = $product->get_id();
if( $id == 1735 && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');

how to change redirect when login in theme my login wordpress

Hello I want to change redirect when login in theme my login wordpress
now when login yet, it will go to profile page but I don't want this. I want to change it to go to my homepage.
I find code in themed-profiles.php . I think I much edit code in line 166 but how to edit? please help.
if ( is_user_logged_in() ) {
//$redirect_to = get_option('shop-subearphone2');
$redirect_to = Theme_My_Login::get_page_link( 'profile' );
wp_redirect( $redirect_to );
exit;
Okay, I got it sorted. I just removed the redirection module from used a WordPress function and put function.php
function redirect_to_profile() {
$who = strtolower(sanitize_user($_POST['log']));
$redirect_to = get_option('home') . '/profile?' . $who;
return $redirect_to;
}
add_filter('login_redirect', 'redirect_to_profile');
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() ); // set here your redirection Url
exit;
}
}
Try this code
if ( is_user_logged_in() ) {
add_action('init','redirect');
function redirect(){
global $pagetheme;
if( 'wp-login.php' == $pagetheme) {
wp_redirect('http://yourpage.com/'); // instead of yourpage.php to what page you want to redirect
}}
}
You can put a custom file in the /wp-content/plugins folder which must be called 'theme-my-login-custom.php' and in there I just put:
<?php
function custom_redirect() {
return "/member";
}
add_filter( 'tml_redirect_url', 'custom_redirect' );
?>
This did the job for me using TML 6.4.5

Woocommerce - Hide shop for unregistered user

I have a woocommerce web site and I would like to hide the shop when the user in not logged. I put this code in the file ! archive-product.php which is in my template 'twentytwelve-child' in a woocommerce folder.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
auth_redirect();
get_header( 'shop' ); ?>
Normaly the 'auth_redirect()' have to redirected me in the login page, but it just doesn't work.
I tried also with this code but it does not work also.
$login = is_user_logged_in();
if ($login == FALSE ) {
wp_redirect( home_url() );
exit;
}
Did I do something wrong?
Thanks you. I also add some other features which can be helpful.
// Redirect none registered users to a login page
function custom_redirect() {
if( (is_shop() || is_product() || is_product_category() ) && ! is_user_logged_in() ) {
wp_redirect( site_url( '/mon-compte' ) );
exit();
}
}
add_action("template_redirect","custom_redirect");
You don't need to modify Woocommerce template file for what you are trying to achieve. Just add the following code to functions.php
function custom_redirect() {
if( is_shop() && ! is_user_logged_in() ) {
wp_redirect( home_url() );
exit();
}
}
add_action("template_redirect","custom_redirect");

Categories