Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Redirect any WordPress attachment page to the actual file
* Redirect all attachment pages to actual file
function redirect_attachment_pages() {
global $post;
// Return if not an attachment
if ( !is_attachment() ) {
if( is_attachment() ) {
$url = wp_get_attachment_url($post->ID);
wp_redirect( $url, 301 );
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