Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redirect any WordPress attachment page to the actual file
<?php
/**
* Redirect all attachment pages to actual file
*/
function redirect_attachment_pages() {
global $post;
// Return if not an attachment
if ( !is_attachment() ) {
return;
}
if( is_attachment() ) {
$url = wp_get_attachment_url($post->ID);
wp_redirect( $url, 301 );
exit();
}
}
add_action( 'template_redirect', 'redirect_attachment_pages' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment