How to change redirection route to a php page for making it only accessible by logged-in members?

by diladadufe   Last Updated May 14, 2019 18:08 PM

Background of my problem: I'd like to mask external download links as internal links and to be only accessible by logged-in wp users at htaccess level or with PHP script but when redirection happens the visitor outside of wordpress can still access download links by pasting direct url into browser bar.

I've tried this code for redirection to external link.

Redirect 301 /resources https://external.com/direct-download-link1

Before accessing that direct download link (ourwebsite.com/resources) a script must be like man in the middle and check if the visitor is logged into wordpress.

I'd like to change where the redirect goes have it go to a PHP page where you may load WordPress and check the role of the user to make sure they are logged in.

require('../wp-load.php');  // modify to reflect where your PHP file is in relation to Wordpress
$roles = wp_get_current_user()->roles;  // get current users role

if (!in_array('alloweduserrole',$roles)) {  // modify to match your roles that are allowed to download

    header('Location: http://www.ourwebsite.com/');
    exit;

}  // end of if user does not have the proper role

The above code can be developed with a simple php checking script. But don't know how to implement and which code to change.



Related Questions


Insert "javascript:void(0);" into URL

Updated July 02, 2019 10:08 AM

altering the htaccess file of my WordPress

Updated May 30, 2015 12:03 PM



Hiding Wordpress Page Url Path

Updated November 01, 2018 10:08 AM