1

Is it possible to password protect a PDF for visitors to our website (not users)? We have uploaded a PDF to our media and created a link to it on a page, but we would like to password protect just that PDF and not the whole page that contains the link to it.

0

an additional solution that may work and require no coding is to insert the PDF into a post and just have the post protected. Then insert the post onto the page

0

you can do this by adding the following code into your themes function file or creating a plugin. This will not display the link unless the user is logged in.

Its not the best way to do this by far as hot linking can still be done by using the direct path to the file. You would require to add code to your .htaccess file to stop hot linking.

The below section makes a shortcode you will need to add this to your themes function file but will be deleted on theme update so creating a plugin is smarter if you use this method.

function foobar_func(){
// Checks if the user is logged in  
if ( is_user_logged_in() ) {
    // Add the files link here

    // ID of an attachment found in the media section googling will show you how to find the PDF id if you don't know.
    $id = 9;
    // Display a link of the media file
    echo wp_get_attachment_link( $id, '' , false, false, 'My link text' );  
} else {
    // You can add a message if you wish to tell people to login to view or delete the line below to remove the text.
    echo 'Login to view file';
}
}
add_shortcode( 'foobar', 'foobar_func' );

You would then add this code into your page text editor where you wish to display the file. Note that this does not stop direct linking to the file but will not display a link to anyone not logged in.

[foobar]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.