Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display WordPress Shortlinks in Your Theme

Last updated on by
Elegant Themes
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>
<?php } ?>

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

Get Shortlinks in WordPress Theme

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:

Code Preview in WordPress Theme

Source: OttoPress


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Numan Zahid says:

    i am using this plugin to generate my shortlinks
    Bit.ly Shortlinks Multisite (Uses OAuth 2 API)
    http://wordpress.org/plugins/bitly-shortlinks-multisite/installation/

    this plugin have its own get function get_bitly($url)

    Can you please tell me how can i embed this shortlink into my template?
    Thanks…

  2. husien says:

    sory i use it in sidebar and
    $id=get_the_ID();
    echo wp_get_shortlink($id,’post’);

    din’t wok why ?

  3. missprogrammer says:

    thanks a lot
    how can i search post from current taxonomies? and if i’m not in taxonomies(when i’m on single.php), search in all taxonomies?
    thanks
    please help me :)

  4. Saurabh says:

    Hello there.
    I am having JetPack which is providing wp.me shortlinks. I don’t wants to show the shorten URL to visitor but I wants, when they share my article on any social network then the short URL will propagate rather than the long %postname% format URL of my theme.

    How to do that? If JetPack make it available then might my theme is not supporting that thing. I think there is something to do with rel=shortlink. Please help.

  5. Randell says:

    This was a wonderful post thank you so much, was first site i looked at in google and i found my answer right away just brilliant this worked for me, im using jet pack on my blog and it’s done the trick thank you so much for a simple way to integrate short links to post page. Just brilliant

  6. JustALilLost says:

    Sorry, I don’t think I have a single.php file. Would I have to access that through the FTP site or can this be added somewhere on the our self-hosted WP dashboard?

    Thanks!

    • Editorial Staff says:

      If you don’t find a single.php file in your theme, this means that you are probably using a child theme. In which case, either you need to override that single.php file by creating a new one in your child theme’s folder. Or, you would need to use the hooks provided by your theme framework.

  7. Delario says:

    @Konstantin I have heard that Goo.gl is not so comfortable

  8. Konstantin says:

    There’s also a Goo.gl plugin for WordPress native shortlinks ;) And yes it’s written by me muwhahaha!

    ~ @kovshenin

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.