Wordpress redirection issue

I have website application
function restrict_access_if_logged_out(){
$redirect = ‘https://example.com/cf/wp-login.php?redirect_to=’ . urlencode($redirct_request_url );
wp_redirect( $redirect );
add_action( ‘wp’, ‘restrict_access_if_logged_out’, 3 );

When I switch website validate and navigate me to https://example.com/cf/cf/wp-login.php?redirect_to=%2Fhero%2F instead of https://example.com/cf/hero

I have check code for the website but its not working as expected.

@vasims - It doesn’t look like you’re checking if the user is logged in or not there so this will redirect everyone, regardless of whether they are logged in or not. You’ll want to add a check like this in there:

if ( ! is_user_logged_in() ) {
	// Redirect goes here. 

I would also recommend using the template_redirect hook instead of wp.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.