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 the following simple steps.

Redirect Users to Files Protected by PDA Gold after Login

Requirements:

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: Redirect users with the following code snippet

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 above 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.

Redirect Users to Files Protected by Folder Protection after Login

Requirements:

Step 1: Protect files under specific folders using our Folder Protection feature.

Step 2: Add the following code snippet to your (child) theme’s functions.php file to redirect users to protected files after logging in.

add_filter( 'pda_ar_render_no_access_page', 'handle_pda_ar_render_no_access_page', 10, 3 );

function handle_pda_ar_render_no_access_page( $is_check, $file_path ) {

    if ( $is_check ) {

        if ( !is_user_logged_in() ) {

            // Input your redirected URL or leave blank to redirect to WordPress login page.
            $url_redirect = '';

            if ( $url_redirect ) {

                wp_redirect($url_redirect);

                exit;

            } else {

                $url_local = site_url().'/'.$file_path;

                wp_safe_redirect( add_query_arg( 'redirect_to', rawurlencode( $url_local ), wp_login_url()) );

                exit;

            }

        } else {

            return true;

        }

    }

    return false;

}

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

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

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, 2022