Skip to content

Instantly share code, notes, and snippets.

@davidsword
Created February 8, 2018 20:56
Show Gist options
  • Save davidsword/b2afdbd1ed48affeea6937c9891ba3c0 to your computer and use it in GitHub Desktop.
Save davidsword/b2afdbd1ed48affeea6937c9891ba3c0 to your computer and use it in GitHub Desktop.
redirect the page based on the shortcode attribute
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.
<?php
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>";
} );
// [pdf_redirect url="http://example.com/file.pdf"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment