WordPress wp_get_shortlink() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a URL

Definition

In WordPress, wp_get_shortlink() returns the short URL of a post, such as https://example.com/?p=12. It does not print.

Use it when you want only the URL, for example to copy it or put it in a meta tag.

Syntax

A few ways to call wp_get_shortlink().

WordPress
<?php
echo esc_url( wp_get_shortlink() );
CallWhat it does
wp_get_shortlink()Short link of the current post.
wp_get_shortlink( 12 )Short link of the post with ID 12.
wp_get_shortlink( 12, 'post' )Use the post context.
the_shortlink()Print a full anchor tag with the short link.

It returns the URL. Use echo esc_url() to print it.

Full declaration
WordPress
wp_get_shortlink( int $id = 0, string $context = 'post', bool $allow_slugs = true ): string

Parameters

ParameterDescription
$idint. A post ID. Default is 0, which uses the current post.
$contextstring. The kind of ID, post or query. Default is post.
$allow_slugsbool. Whether a plugin may replace the ID with a slug. Default is true.

Return Value

Return ValueDescription
stringThe short URL. It can be an empty string if there is none.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Print the short URL

Print the short link for the current post.

WordPress
<?php echo esc_url( wp_get_shortlink() ); ?>

How It Works

  1. wp_get_shortlink() returns the short URL.
  2. esc_url() makes it safe.
  3. The number is the ID of the post.

Example 2 — Short link of another post

Pass a post ID.

WordPress
<?php
$url = wp_get_shortlink( 42 );
echo 'Share: ' . esc_url( $url );

How It Works

  1. 42 is a post ID.
  2. You do not need The Loop.
  3. We join the URL to some text.

Example 3 — Add a shortlink tag to the page head

Print a link tag in the head for tools that read it.

WordPress
<?php
function mytheme_shortlink_tag() {
  if ( is_singular() ) {
    $url = wp_get_shortlink();
    if ( $url ) {
      echo '<link rel="shortlink" href="' . esc_url( $url ) . '">' . "\n";
    }
  }
}
add_action( 'wp_head', 'mytheme_shortlink_tag' );

How It Works

  1. The wp_head hook runs in the page head.
  2. is_singular() limits the tag to single posts and pages.
  3. WordPress already adds this tag by default, so you only need this when a theme removed it.

Frequently Asked Questions

It returns the short URL of a post as a string.
By default it looks like https://example.com/?p=12.
the_shortlink() prints a full anchor tag. wp_get_shortlink() returns only the URL.
No, if you pass a post ID.
Yes. Use esc_url() when you print it.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful