Skip to content

Instantly share code, notes, and snippets.

@norcross
Created August 10, 2012 03:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/3310808 to your computer and use it in GitHub Desktop.
Save norcross/3310808 to your computer and use it in GitHub Desktop.
create clean attachment URLs
// clean URLs for attachments
// http://domain.com/?attachment_id=173
// http://domain.com/attachment/173
function rkv_attachment_urls($wp_rewrite){
$new_rules = array();
$new_rules['media/(\d*)$'] = 'index.php?attachment_id=$matches[1]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function rkv_flush_rules() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['media/(\d*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_action('generate_rewrite_rules', 'rkv_attachment_urls');
add_action('wp_loaded', 'rkv_flush_rules');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment