WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
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

WPBeginner» Blog» Themes» How to Display WordPress Shortlinks in Your Theme

How to Display WordPress Shortlinks in Your Theme

Last updated on June 22nd, 2012 by Editorial Staff
21 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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

21 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

11 Comments

Leave a Reply
  1. Numan Zahid says:
    Feb 11, 2014 at 12:26 am

    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…

    Reply
    • Numan Zahid says:
      Feb 11, 2014 at 12:35 am

      Also tell me how to apply some CSS class to this function… :)

      Reply
  2. husien says:
    Nov 28, 2013 at 7:33 am

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

    din’t wok why ?

    Reply
    • WPBeginner Support says:
      Nov 28, 2013 at 10:02 am

      Husein get_the_ID needs to be inside the loop.

      Reply
  3. missprogrammer says:
    Nov 4, 2013 at 10:07 am

    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 :)

    Reply
  4. Saurabh says:
    Feb 3, 2013 at 12:16 pm

    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.

    Reply
  5. Randell says:
    Dec 10, 2012 at 6:12 am

    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

    Reply
  6. JustALilLost says:
    Jul 2, 2012 at 4:17 pm

    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!

    Reply
    • Editorial Staff says:
      Jul 2, 2012 at 4:49 pm

      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.

      Reply
  7. Delario says:
    Feb 3, 2011 at 6:28 am

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

    Reply
  8. Konstantin says:
    Feb 3, 2011 at 4:38 am

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

    ~ @kovshenin

    Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WP Mail SMTP logo
WP Mail SMTP
Fix WordPress email delivery issues. #1 SMTP plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
RafflePress - WordPress Giveaway and Contest Plugin
RafflePress Coupon
Get 20% off RafflePress, the best WordPress giveaway and contest plugin available in the market.
Bluehost Logo
Bluehost Coupon
Get over 69% off on Bluehost web hosting packages plus a free domain & free SSL certificate. Just $2.75/month.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.