Created
August 10, 2012 03:48
-
-
Save trepmal/3310841 to your computer and use it in GitHub Desktop.
Pretty Attachment Links
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 | |
//Plugin Name: Pretty Attachment Links | |
//change attachment link format | |
add_filter('attachment_link', 'pal_change_link', 10, 2 ); | |
function pal_change_link( $link, $id ) { | |
return home_url("/media/$id/"); | |
} | |
// Adding a new rule | |
add_filter('rewrite_rules_array', 'pal_insert_rewrite_rules'); | |
function pal_insert_rewrite_rules( $rules ) { | |
$newrules = array(); | |
$newrules['media/(\d*)$'] = 'index.php?attachment_id=$matches[1]'; | |
return $newrules + $rules; | |
} | |
// flush_rules() if our rules are not yet included | |
add_action('wp_loaded', 'pal_flush_rules'); | |
function pal_flush_rules() { | |
$rules = get_option( 'rewrite_rules' ); | |
if ( ! isset( $rules['media/(\d*)$'] ) ) { | |
global $wp_rewrite; | |
$wp_rewrite->flush_rules(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment