How to mask external download links in all posts and restrict to members?

1.4k Views Asked by At

How to mask external download links as internal links and to be only accessible by logged-in users? And even logged-in members shouldn't see the external download site domain if they logged-in and looked at posts. It should be masked for everyone. Plus they shouldn't get access to download url if they copy/paste the url into their browser if they don't logged-in to wordpress site.

Like the direct download link example here: www.externallink.com/direct-download-link1

We want to mask it like as our internal url: www.ourwebsiteurl.com/direct-download-link1

and this above link should only be accessible by logged-in members.

How to do?

We've tried below codes inside functions.php and single post.php. But nothing happened, nothing changed. We have full control of external download link site. And it is perl based script which generates different download links.

$user = wp_get_current_user();
if( $user->exists ) {
    add_filter( 'public_link_root', function() { return 'example.com'; } );
}
$some_link = apply_filters('public_link_root', 'ourwebsite.com') . '/resources';
echo '<a href="' . $some_link . '">View Resources</a>';

or this one:

$link_to_output = apply_filters( 'public_link_root', 'ourwebsite.com' ) . '/resources/whatever/here';

or in functions.php:

function custom_link() {
     $user = wp_get_current_user();
     return (is_user_logged_in())? "www.example.com/direct-download-link1":"just_a_dummy_link";
}

single.php:

echo '<a href="' . custom_link() . '">View Resources</a>';

I expected to all urls of www.externallink.com domain will be changed to www.ourwebsiteurl.com but nothing changed with above codes. The links should be masked for even logged-in members. And those links shouldn't be work if they copy and paste those urls into their browser if they are not logged-in.

1

There are 1 best solutions below

14
chatnoir On

Update

OP's comments: "What about redirection? It can be seen i know i just wanted at least hide those urls so most of them wont know it."

The easiest way is to edit the .htaccess file on your server

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

Edited

Perhaps you can create your own custom function using is_user_logged_in() (see doc). Anywhere you want to put a link, call your custom function:

So in functions.php:

function custom_link() {
    // will only show link when user is logged in
    if (is_user_logged_in()) {
        // this only shows the "internal" link which will be redirected to external link
        echo '<a href="https://www.example.com/resources">View Resources</a>';
    }
}

And on your page:

   custom_link(); //will not show link if user not logged in