Last active
October 7, 2023 18:04
-
-
Save tarecord/fd4743eb658998551ab4843b65de8f44 to your computer and use it in GitHub Desktop.
Redirect any WordPress attachment page to the actual file
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 | |
/** | |
* 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