Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. function sar_attachment_redirect() {
  2. global $post;
  3. if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) {
  4. wp_redirect(get_permalink($post->post_parent), 301); // permanent redirect to post/page where image or document was uploaded
  5. exit;
  6. } elseif ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent < 1) ) { // for some reason it doesnt works checking for 0, so checking lower than 1 instead...
  7. wp_redirect(get_bloginfo('wpurl'), 302); // temp redirect to home for image or document not associated to any post/page
  8. exit;
  9. }
  10. }
  11. add_action('template_redirect', 'sar_attachment_redirect',1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement