Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created August 28, 2011 19:35
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 chrisguitarguy/1177112 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1177112 to your computer and use it in GitHub Desktop.
Ridirect Wordpress attachment pages
<?php
/*
Plugin Name: Kill Attachment Pages
Plugin URI: http://pmg.co/
Description: Redirects all attachment pages to their parent page
Version: 1.0
Author: Christopher Davis
Author URI: http://pmg.co/people/chris
License: creative commons/GPL2
*/
add_action( 'template_redirect', 'wpse27119_template_redirect' );
function wpse27119_template_redirect()
{
// get out of here if we're not headed to an attachment page
if( ! is_attachment() ) return;
// Find the $post variable representing the page where we're heading
global $post;
if( empty( $post ) ) $post = get_queried_object();
// Find the permalink of our parent post
$link = get_permalink( $post->post_parent );
// redirect to the parent page
wp_redirect( $link, '301' );
exit(); // always call exit after wp_redirect
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment