[WordPress] shortlink
2009.10.22 12:01
function add_head_shortlink(){
global $post;
if ( is_singular() ) {
$siteurl = trailingslashit(get_option('siteurl'));
$hex_id = dechex($post->ID);
echo "<link rel=\"shortlink\" href=\"$siteurl$hex_id\" />\n";
}
}
add_action('wp_head', 'add_head_shortlink');
function redirect_shortlink($redirect_url, $requested_url){
if ( !empty($redirect_url) ) return $redirect_url;
$siteurl = trailingslashit(get_option('siteurl'));
$pattern = '/' . preg_quote($siteurl, '/') . '([\da-f]+)$/i';
if ( preg_match_all( $pattern, $requested_url, $matches ) ) {
$post_id = $matches[1][0];
$redirect_url = get_permalink(hexdec($post_id));
if ( empty($redirect_url) ) $redirect_url = get_permalink($post_id);
}
return $redirect_url;
};
add_filter('redirect_canonical', 'redirect_shortlink', 10, 2 );
posted by wokamoto1973
http://quill.to/wokamoto1973/75bd3e1069
