Skip to content

Instantly share code, notes, and snippets.

@tarecord
Last active October 7, 2023 18:04
Show Gist options
  • Save tarecord/fd4743eb658998551ab4843b65de8f44 to your computer and use it in GitHub Desktop.
Save tarecord/fd4743eb658998551ab4843b65de8f44 to your computer and use it in GitHub Desktop.
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