I’ve found myself tinkering with WordPress stuff a lot lately. Here’s another quick change I made for nicer shortlinks with a shorter domain that I have.
This was inspired by trawling through jwz’ archives and hacks. This post in particular.
The first piece of this puzzle is adding this location block to the nginx configs for bhh.sh. There’s probably a better way to do this to handle more types or remove the extra redirect.
location ~* /p/(.*) {
return 302 https://benharri.org?p=$1;
}
The other half is filtering the shortlink on the WordPress end. I’ve added this to the functions.php of my theme. Basic logic here is based on jwz’ base64 shortlinks, just minus the base64 and dumping the post ID right in there.
add_filter( 'pre_get_shortlink', 'bhhsh_shortlink', 10, 4 );
function bhhsh_shortlink( $shortlink, $id, $context, $allow_slugs ) {
if ($context == 'query' && !is_singular())
return false;
$post = get_post( $id );
if ( empty( $post ) )
return false;
$id = $post->ID;
if ( empty( $id ) )
return false;
return 'https://bhh.sh/p/' . $id;
}
One reply on “Marginally Better Shortlinks”
it was benharri.org/?p=2291 which is uglier and uses the long domain