0

I am wondering if it is possible to redirect a user after they enter a password on a password protected page to a PDF?

So instead of showing the content of that page, I'd like to just load a PDF in the browser window. I would rather not embed the PDF on the WordPress page.

Any ideas?

1
  • you can create a role with only the read capabilitie to use the backend. and there you add a page which test the connected user and generate the PDF.
    – mmm
    Commented Dec 15, 2017 at 18:38

1 Answer 1

0

Because the_content doesn't even process until the password has been successfully entered, you can add a shortcode that redirects the page, that shortcode will run only with good password. So, something like the following should work:

add_shortcode( 'pdf_redirect', function ( $atts ) {
    $a = shortcode_atts( array(
        'url' => '',
    ), $atts );

    // instructions in case of misuse
    if (empty($a['url']))
        return "<pre>Please assign a destination for the PDF redirect, [pdf_redirect url='https://yourpdfurl...']</pre>";

    return "
    <meta http-equiv='refresh' content='0; url={$a['url']}'>
    <script>
        window.location.href = '{$a['url']}';
    </script>
    <p>If your browser hasn't redirected you, please <a href='{$a['url']}'>click here</a>.</p>";
} );

And then on your password protected page, the content is:

[pdf_redirect url="http://example.com/file.pdf"]

The meta refresh is bad coding standards, but all browsers support it and it works. The javascript is fallback redirect, and the html link is double-fallback. Now you could make the redirect happen before the document headers by tapping into a early hook like wp_loaded, seeing if the user has proper password with a function or two, and interpret the $post->post_content shortcodes yourself then properly redirecting with wp_redirect() - but the lazy way I've done works too.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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