wp redirect to same page in php - php

on my website I restricted 3 pages (IDs 861, 869, 931) to all non-registered users but I'm having a problem, those pages will always redirect the user to the login page (even if I'm logged in), what I'm doing wrong?
Here's the code, a little help would be greatly apprecieated
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_single('861') or is_single('869') or is_single(931) &&
!is_user_logged_in() ) {
wp_redirect( 'http://mywebsite.com/login', 301 );
exit;
}
}

It looks like you need to scope your order of operations a little better. You first need to check if you're on any of those pages, and THEN if the user is logged in. So:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( (is_single('861') || is_single('869') || is_single(931)) &&
!is_user_logged_in() ) {
if ( !is_user_logged_in() ) {
wp_redirect( 'http://mywebsite.com/login', 301 );
exit;
}
}
Better yet, you could pass those IDs into the same is_single() call:
if ( is_single(array(861, 869, 931)) && !is_user_logged_in() ) {
if ( !is_user_logged_in() ) {
wp_redirect( 'http://mywebsite.com/login', 301 );
exit;
}
}
By the way, why are you using is_single() if you're checking pages? Are these custom post types? You might be better off with is_page().

if ( is_single('861') or is_single('869') or is_single(931) &&
!is_user_logged_in() )
Shouldn't it be more like?
if ( (is_single('861') || is_single('869') || is_single(931)) &&
!is_user_logged_in() )
C# conditional AND (&&) OR (||) precedence
In C# && apparently has higher precedence than ||. Better put it into () just in case.
http://php.net/manual/en/language.operators.precedence.php
I mean, maybe PHP too.

You if conditional is incorrect. Try this code
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_single('861') || is_single('869') || is_single(931) ) {
if ( !is_user_logged_in() ) {
wp_redirect( 'http://mywebsite.com/login', 301 );
exit;
}
}

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 can i force visitors in woocommerce to login before they can access my shop?

The below code does not work. It works only for the cart and the checkout for some reason. My guess is that is_shop returns false. I tried to hook it to template_redirect but again is not working properly. Help?
function woo_login_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_shop() || is_cart() || is_checkout())
) {
wp_redirect( '/ο-λογαριασμός-μου/' );
exit;
}
}
add_action('template_redirect', 'woo_login_redirect');
function woo_login_redirect() {
if (! is_user_logged_in() && is_page('your page id')) {
wp_redirect( '/ο-λογαριασμός-μου/' );
exit;
}
}
add_action('template_redirect', 'woo_login_redirect');
Please visit this link

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');

Restricting user access to a particular page - Wordpress

I've got this brilliant piece of code that restricts access to the wp-admin login page unless the user logged in is an admin to the site:
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
I want to add another function that restricts access and performs the same redirect to my home page BUT for a specific page of my site.
But this isn't working:
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'mysite.co.uk/shop' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
And I'm not surpised by that but I was hoping somebody had to correct code to perform this function?
Thanks
You can add this to your header.php file to check if they're on the page and if they're an admin or not
if(is_page(PAGE_ID)){
current_user_can( 'manage_options' ){
---do redirect here or whatever else---
}
}
You can also create load the function via the init action like you did.

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