Admin-ajax.php 302 error Wordpress - php

I have a Wordpress site and 2 kinds of users. Admins and subscribers. If a subscriber tries to access the wp-admin area I want him to redirect to the home page of the site. I have a function to do this but when I try to access a different page on the site, for example to post a comment somewhere, it loads the home page inside the page I currenly am and doesn't post the comment and any other action I do. From the network errors I see admin-ajax.php 302 error. I understand that Wordpress uses two different hooks for ajax, one for admin side and one for logged out user and it has something to do with my problem but I don't understand how to fix it.
function mt_redirect_admin(){
if ( ! current_user_can( 'edit_events' ) ){
wp_redirect( site_url() );
exit;
}
}
add_action( 'admin_init', 'mt_redirect_admin' );
Thanks in advance for any answers

Solved:
function mt_redirect_admin() {
if ( ! current_user_can( 'edit_events' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
wp_redirect( site_url() );
exit;
}
}
add_action( 'admin_init', 'mt_redirect_admin', 1 );

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

Best way to redirect user to login URL in WordPress?

I have numerous pages that require my users to be logged in when accessing them.
I've remapped my login page to mysite.com/login. So, currently for any page that requires user to be logged in, I would redirect them using the below.
//If user is not logged in...
if ( !is_user_logged_in() ) {
wp_redirect( wp_login_url() );
}
My question is, is the above good enough to ensure non-logged in users will always be redirected and not causing "too many redirect" errors.
Here are 2 examples please check you can add on function.php file
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 ID here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/your-page/' );
exit;
}
}
Put this in functions.php file and change the page ID or slug and the redirect url.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('slug') && ! is_user_logged_in() ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
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();
}
}
}

Redirect to specific page if not logged in WordPress

I am trying to have it on my WordPress site to redirect to a landing page if the user is not logged in, but it always gets stuck in a redirect loop. This is basically what I have (in functions.php)...
add_action( 'template_redirect', function() {
if ( !is_user_logged_in() && ! is_page('login-to-view') ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});
I know I am late for the party. I heard Big and Pac were rocking hard...
Anyways, this is what I usually do:
Create a login page, e.g. www.site.com/login
In my functions.php I add the following code:
add_action( 'template_redirect', function() {
if( ( !is_page('login') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/login' ) ); // redirect all...
exit();
}
}
});
Explanation:
What happens above is access to every page on the site is restricted except for the login page. You can choose to exclude or include additional pages if you want and also the wp-admin URLs as well.
The issue is that I had the function in functions.php. is_page didn't know what page it is on yet. I moved it to the header, just before the doctype declaration and it works now with just this...
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
Another thing to note... I wasn't able to get it to work on my localhost, I'm thinking because site_url can't be found, and it was just redirecting to localhost:8888/login-to-view
I'm using MAMP, but I'm guessing you can set up URL paths like that in the PRO version.
Let's say the user is not logged in.
Then your code thinks in this way:
Is user logged in?
NO? Then do wp_redirect.
The user gets redirected to your page, and the process begins again. That's why you get the infinite loop.
Try altering the order of checks instead:
<?php
add_action( 'template_redirect', function() {
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});

Redirect to front page wordpress if not logged in

I'm looking to redirect a user to a specific page if they are not logged in.
So blog.com/welcome
So they can either be viewing this page or wordpress login pages.
I was trying to modify this plugin
http://wordpress.org/plugins/redirect-to-login-if-not-logged-in/
add_action( 'wp', 'dmk_not_loggedin_redirect' );
function dmk_not_loggedin_redirect() {
global $pagenow;
However I am ending up with redirect loops. And I could use some help to stop this.
if ( ! is_user_logged_in() && $pagenow != 'wp-login.php' or 'http://www.blog.co.uk/front' )
wp_redirect( 'http://www.blog.co.uk/front' , 302 ); exit; }
I tried with the action hook init but the current post/page information is not available yet. But then, template_redirect does the job and it only acts on the front-end, login and admin pages work normally.
Here, there's a page with the slug welcome, to check for a post use is_single('post-slug'):
add_action( 'template_redirect', 'redirect_so_18688269' );
function redirect_so_18688269()
{
if( !is_user_logged_in() && !is_page( 'welcome' ) )
{
wp_redirect( site_url( 'welcome' ) );
exit();
}
}

Categories