Last active
April 26, 2020 15:35
-
-
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
This file contains hidden or 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 | |
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