How to Display WordPress Shortlinks in Your Theme

Have you ever seen sites that have a little input field at the bottom of their post with a short link to their article? Well usually that shortlink is coming from the WordPressΒ get_shortlinkΒ filter. In this article, we will show you how you can use the get_shortlink codes to display a little input box in your post. It will also allow your user to select the shortlink on one click which makes it easy for them to copy and paste it.

All you have to do is put this code somewhere in the loop (usually in single.php) file.

<?php if (function_exists(‘wp_get_shortlink’)) { ?>
<div><span class=”post-shortlink”>Shortlink:
<input type=’text’ value='<?php echo wp_get_shortlink(get_the_ID()); ?>’ onclick=’this.focus(); this.select();’ />
</span></div

This will bring you the same shortlink that you see when you click the Get Shortlink Button below your post title.

By default it is yoursite.com/?p=1082 or something similar. But that can be replaced with other services by using any shortlink plugin that uses the β€œget_shortlink” filter to override the shortlink properly likeΒ WordPress.com StatsΒ for (wp.me) orΒ WP BitlyΒ for (Bit.ly or Bit.ly PRO)

Once you have the code added in your theme, the preview would look like this:

Source:Β OttoPress

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!

Pin It on Pinterest

Add address