wp_get_shortlink()
Returns a shortlink for a post, page, attachment, or site.
This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts. Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output via the get_shortlink filter.
Hooks from the function
Return
String
. A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
Usage
wp_get_shortlink( $id, $context, $allow_slugs );
- $id(int)
- A post or site ID.
Default: 0, which means the current post or site - $context(string)
- Whether the ID is a 'site' ID, 'post' ID, or 'media' ID. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the ID and context.
Default: 'post' - $allow_slugs(true|false)
- Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
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. |