Created
August 28, 2011 19:35
-
-
Save chrisguitarguy/1177112 to your computer and use it in GitHub Desktop.
Ridirect Wordpress attachment pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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