wp_redirect goes to infinity loop - wordpress

I need to check for logged-in users & redirects to pages with wp_redirect, but it goes through redirected many times
I have tried with template_redirect & init hooks
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( !is_user_logged_in() ) {
wp_redirect( 'http://localhost/lawyer_portal/login/' );
exit;
}
}

Try the follows code snippet -
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
// Assume that url login slug is a page
if ( !is_user_logged_in() && !is_page( 'login' ) ) {
wp_redirect( 'http://localhost/lawyer_portal/login/' );
exit;
}
}

Related

Non logged in user when try to see a product page will get Login page and after login will be redirected to the Product page that user clicked before

I want to give access Woo product single page only for Logged in users.
I have a product listing on the Homepage and Shop page, What I want is: when a logged out user click on a Product, the user will get a Login form and after Logged in user will be redirected to the Product page that he wanted to View.
So the flow will be something like:
Home/Shop-> click to Product X -> Login page -> Redirect to Product X single page
Currently, I am using the regular Woo Login form created by this function
woocommerce_login_form()
I am trying bellow code snippets:
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
wp_safe_redirect($location);
exit();
}
}
add_action('init','my_login_redirect');
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
//wp_safe_redirect($location);
}
-----------------------
AND ALSO THIS ONE
-----------------------
function redirect_after_login(){
global $wp;
$protocol='http';
if (isset($_SERVER['HTTPS']))
if (strtoupper($_SERVER['HTTPS'])=='ON')
$protocol='https';
if (!is_user_logged_in() && is_product() ){
$redirect = site_url() . "/my-account.php?redirect_to= $protocol://" .
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( 'wp', 'redirect_after_login', 3 );
In both of cases, the problem is, it always find the Login page as HTTP_REFERER / REQUEST_URI
Because currently, I am using below code to redirect Non-logged-in user who is trying to see the product page to the Login page:
add_action('template_redirect', 'ethis_redirect_for_loggedin_users');
function ethis_redirect_for_loggedin_users() {
if ( !is_user_logged_in() && is_product() ) {
wp_redirect(site_url().'/default-login');
exit;
}
}
You can use the template_redirect filter hook to prevent guest users to access a single product page.
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && is_singular( 'product' ) ) {
global $post;
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')).'?redirect='.get_the_permalink( $post->ID ) );
exit();
}
}
Then after you have to use the woocommerce_login_redirect filter hook for login redirect.
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 2 );
function my_login_redirect( $redirect, $user ) {
if( isset( $_GET['redirect'] ) && $_GET['redirect'] != '' ){
return $_GET['redirect'];
}
return $redirect;
}
Code will go in your active theme functions.php Tested and Works.

In Woocommerce -Redirect to shop page after removing last item from the cart page when item is removed through ajax

How to redirect page to shop page when last item removed from cart in woocommerce using ajax?
I am tried below code:
function cartitem_after_remove_product($cart_item_key) {
global $woocommerce;
$total_count = $woocommerce->cart->cart_contents_count;
if($total_count == 0)
{
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
}
}
add_action( 'woocommerce_cart_item_removed', 'cartitem_after_remove_product' );
You have to add this code into you function.php file (your theme):
function cart_empty_redirect_to_shop() {
global $woocommerce, $woocommerce_errors;
if ( is_cart() && sizeof($woocommerce->cart->cart_contents) === 0) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
add_action( 'template_redirect', 'cart_empty_redirect_to_shop' );
You can do this via Javascript. WooCommerce has several custom events built in. Your own script can listen to these events and run your own code when they are triggered. One idea would be to check for the class "cart-empty" on updated_cart_totals.
$( document.body ).on( 'updated_cart_totals', function(){
// Check for empty class
if ( $('div.woocommerce-info').hasClass( "cart-empty" ) ) {
window.location.href = "http://www.example.com/shop/";
}
});

How do I add a wordpress notice message after page redirect?

The following example will redirect all non-authenticated users to a custom 'signup' page when trying to visit the 'goodies' page.
however, I need to display a notification message on the signup page after redirect, What is the best way to do this?
function my_page_template_redirect() {
if ( is_page( 'goodies' ) && ! is_user_logged_in() ) {
wp_redirect( home_url( '/signup/' ) );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
here is an example of the code that displays the message
add_action( 'woocommerce_init', 'pt_custom_notice' );
function pt_custom_notice() {
wc_add_notice( 'This is a Error notice', 'error' );
}

Redirect to URL if the user isnt the author of the post

I am looking for a way to redirect when viewing any publication, except the author of the publication.
There are two roles in the "author" and "custom_role" site. This last role is allowed to see all.
the role of author can only see his own, the rest redirects.
I've tried for a while, in this last code I'm working but it does not work and I do not know why
Thanks very much!
add_action( 'pre_get_posts', function() {
if( is_author() )
{
if( ! is_user_logged_in() )
{
wp_redirect( 'https://aaa.com/custom' );
exit;
}
$author = get_queried_object();
if( $author->ID != get_current_user_id() )
{
wp_redirect( get_author_posts_url(
get_current_user_id() ) );
exit;
}
}
} );
First of all, is_author() is used to check if current page is author archive page. Please check following example. This may not be the exact anster but it may help. In the single post, post author and current user ID is compared. If they are not same then it is redirected to home page. If those IDs are same then, current user is also the post author, so current user will be allowed to view page.
add_action( 'get_header', 'wpso_author_redirection' );
function wpso_author_redirection() {
if ( is_singular() ) {
$current_post_details = get_post( get_the_ID(), ARRAY_A );
$user_id = get_current_user_id();
if ( $user_id !== absint( $current_post_details['post_author'] ) ) {
wp_redirect( home_url() );
}
}
}

How to Automatically Remove Default Video Links in WordPress

I am looking for the user to redirect to the home page when the user tries to access the video permalink.
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);
I tried the above code, but it doesn't work
Actually you need to redirect the users via template_redirect hook that you can use as per below.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
$image_set = get_option( 'image_default_link_type' );
if ( is_page($image_set) && ! is_user_logged_in() ) {
wp_redirect( 'http://www.yourhomepage.com/', 301 );
exit;
}
}
Check the $image_set is getting correct URL.

Resources