Skip to content

Instantly share code, notes, and snippets.

@krasenslavov
Last active April 26, 2020 15:35
Show Gist options
  • Save krasenslavov/cc767207d56df96fe905fe753acf1aca to your computer and use it in GitHub Desktop.
Save krasenslavov/cc767207d56df96fe905fe753acf1aca to your computer and use it in GitHub Desktop.
Use Bitly to generate short URLs in WordPress. Visit blog post https://bit.ly/2XB3M8s
<?php
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
add_action('wp_head', function() {
global $post;
$short_url = get_post_meta($post->ID, 'bitly_url', true);
if(!empty($short_url)) {
echo '<link rel="shortlink" href="' . $short_url . '" />';
} else {
echo '<link rel="shortlink" href="' . get_bloginfo('url') . '?p=' . $post->ID . '" />';
}
}, 10, 1);
add_action('pre_get_shortlink', function() {
global $post;
$short_url = get_post_meta($post->ID, 'bitly_url', true);
if(!empty($short_url)) {
return $short_url;
} else {
return get_bloginfo('url') . '?p=' . $post->ID;
}
}, 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment