wp_get_shortlink()WP 3.0.0

Returns a short link to a post.

This function exists to create a short, immutable link in templates and plugins, which can be used regardless of the installed permalink type.

This template tag is intended to obtain a short link to a post/blog when permalinks (human-readable URLs) are enabled on the blog. Such a short link is convenient for posting notes on social networks (twitter).

Such short external links do not negatively affect search engine optimization (SEO) because when following such a link, the search engine bot is redirected to the normal URL using a 301 redirect (indicating that the page has moved), resulting in all the weight being transferred to the original page.

Hooks from the function

Returns

String. A short link or an empty string if a short link does not exist for the requested resource, or if the link is not available.

Usage

echo wp_get_shortlink($id, $context, $allow_slugs);
$id(integer)
ID of the post or blog. Default is 0, meaning the current blog or post is used.
Default: 0 (current post)
$context(string)

Explanation of which ID is specified in the $id parameter:

  • post - post ID;
  • blog - blog ID;
  • media - media file;
  • query - a short link of the current query will be output (the parameters $id and $context will be obtained from the current query). If post is specified (by default), the post type will be set automatically.

Default: 'post'

$allow_slugs(boolean)
Whether to allow the use of slugs (alternative names) in links. This parameter is intended for hooks and plugins.
Default: true

Examples

0

#1 Display a short link to the current post:

echo 'Short link: '. wp_get_shortlink();

// get
// Short link: http://example.com/?p=1234
0

#2 Remove the short link from the HTML <head>

This example shows how to remove the short link from the HEAD part of the document and from the server response headers, where the short link is also added:

remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );

Changelog

Since 3.0.0 Introduced.

wp_get_shortlink() code WP 6.9

function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
	/**
	 * Filters whether to preempt generating a shortlink for the given post.
	 *
	 * Returning a value other than false from the filter will short-circuit
	 * the shortlink generation process, returning that value instead.
	 *
	 * @since 3.0.0
	 *
	 * @param false|string $return      Short-circuit return value. Either false or a URL string.
	 * @param int          $id          Post ID, or 0 for the current post.
	 * @param string       $context     The context for the link. One of 'post' or 'query',
	 * @param bool         $allow_slugs Whether to allow post slugs in the shortlink.
	 */
	$shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );

	if ( false !== $shortlink ) {
		return $shortlink;
	}

	$post_id = 0;
	if ( 'query' === $context && is_singular() ) {
		$post_id = get_queried_object_id();
		$post    = get_post( $post_id );
	} elseif ( 'post' === $context ) {
		$post = get_post( $id );
		if ( ! empty( $post->ID ) ) {
			$post_id = $post->ID;
		}
	}

	$shortlink = '';

	// Return `?p=` link for all public post types.
	if ( ! empty( $post_id ) ) {
		$post_type = get_post_type_object( $post->post_type );

		if ( 'page' === $post->post_type
			&& 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID
		) {
			$shortlink = home_url( '/' );
		} elseif ( $post_type && $post_type->public ) {
			$shortlink = home_url( '?p=' . $post_id );
		}
	}

	/**
	 * Filters the shortlink for a post.
	 *
	 * @since 3.0.0
	 *
	 * @param string $shortlink   Shortlink URL.
	 * @param int    $id          Post ID, or 0 for the current post.
	 * @param string $context     The context for the link. One of 'post' or 'query',
	 * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
	 */
	return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
}