wp_get_shortlink()
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). Ifpostis 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
#1 Display a short link to the current post:
echo 'Short link: '. wp_get_shortlink(); // get // Short link: http://example.com/?p=1234
#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. |