How to Redirect Users back to Protected Files after Login

Prevent Direct Access (PDA) Gold allows you to grant your protected files access to some specific users or user roles only. Unauthorized people will see your website 404 page or a customized “No Access” Page.

In many cases, you would like users to log in in order to access these files. Once they log in, you want to redirect them back to your protected file URL.

In this article, we will show you how to achieve all of that with 2 simple steps.

Redirect users to protected files after login

Step 1: Protect your private file with PDA Gold and select “Logged-in users” as file access permission.

Only “Logged-in users” are able to access this file

Step 2: Put this custom code snippet on your functions.php file.

After users log in to your website, they will be redirected to your homepage by default. If you want to redirect them to the protected files that they have just tried to access, put the custom code snippet below at the bottom of your (child) theme’s functions.php file:

Note: The filter and function will override our “No Access Page” settings option. In case you want to redirect users to a custom login page, you should modify line 8 accordingly.

Non-logged-in users will see the login page when trying to access the protected file

You may want to refer to our blog post to understand more about how to redirect users back to your protected files after login.

Known conflict

WordPress redirect plugins such as Sky Login Redirect might cause our custom function not to work properly. Due to duplicate redirect function, users won’t be redirected back to protected files after login.

To avoid this kind of conflict, simply add the following code snippet into your child theme’s functions.php file. This function will prevent other plugins from attempting to redirect private files protected by PDA Gold.

add_filter( 'login_redirect', 'pda_login_redirect', 99999, 2 );
function pda_login_redirect ( $redirect_to, $redirect ) {
	if ( strpos( $redirect, 'wp-content/uploads/_pda/') ) {
		return $redirect;
	}
	return $redirect_to;
}
Lasted updated on August 19, 2020