Created
June 26, 2012 06:47
-
-
Save imjjss/2993868 to your computer and use it in GitHub Desktop.
attachment rewrite
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
add_filter( 'generate_rewrite_rules', 'press_attachment_rewrite' ); | |
add_filter( 'template_redirect', 'press_redirect_old_attachment' ); | |
/* add new rewrite rule */ | |
function attachment_rewrite( $wp_rewrite ) { | |
$rule = array( | |
'media/(.+)' => 'index.php?attachment=' . $wp_rewrite->preg_index(1) | |
); | |
$wp_rewrite->rules = $rule + $wp_rewrite->rules; | |
} | |
/* redirect standard wordpress attachments urls to new format */ | |
function redirect_old_attachment() { | |
global $wp; | |
if( !preg_match( '/^media\/(.*)/', $wp->request ) && isset( $wp->query_vars['attachment'] ) ) { | |
wp_redirect( site_url( '/media/' . $wp->query_vars['attachment'] ) , 301 ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment