Skip to content

Instantly share code, notes, and snippets.

@thisolivier
Last active June 4, 2019 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisolivier/1365e954bdf6a64c2d336f2bd2828081 to your computer and use it in GitHub Desktop.
Save thisolivier/1365e954bdf6a64c2d336f2bd2828081 to your computer and use it in GitHub Desktop.
Wordpress - No more media attachment or author pages
/**
* Redirect away from attachment and author pages
* Add this snippet to your functions or filters php files, as appropriate (if in doubt, the bottom of functions.php will do)
* Feel free to message thisOlivier on github/stackOverflow if you get stuck
*
*/
add_action('template_redirect', function() {
if (is_attachment() || is_author()) {
global $post;
if ($post && $post->post_parent) {
wp_redirect(esc_url(get_permalink($post->post_parent)), 301);
return;
} else {
wp_redirect(esc_url(home_url('/')), 301);
return;
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment