wp_get_shortlink

Returns a shortlink for a post, page, attachment, or site.

Description

(string) wp_get_shortlink( (int) $id = 0, (string) $context = 'post', (bool) $allow_slugs = true ); 

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 filter or filter the output via the filter.

Returns (string)

A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.

Parameters (3)

0. $id — Optional. (int)
A post or site id. Default is 0, which means the current post or site.
1. $context — Optional. (string) => 'post'
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,.
2. $allow_slugs — Optional. (bool) => true
Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this. Default true.

Usage

  1. if ( !function_exists( 'wp_get_shortlink' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. A post or site id. Default is 0, which means the current post or site. 
  5. $id = -1; 
  6. $context = 'post'; 
  7.  
  8. // Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how 
  9. // and whether to honor this. Default true. 
  10. $allow_slugs = true; 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = wp_get_shortlink($id, $context, $allow_slugs); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { 
  2. /** 
  3. * Filters whether to preempt generating a shortlink for the given post. 
  4. * Passing a truthy value to the filter will effectively short-circuit the 
  5. * shortlink-generation process, returning that value instead. 
  6. * @since 3.0.0 
  7. * @param bool|string $return Short-circuit return value. Either false or a URL string. 
  8. * @param int $id Post ID, or 0 for the current post. 
  9. * @param string $context The context for the link. One of 'post' or 'query',  
  10. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. 
  11. */ 
  12. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs ); 
  13.  
  14. if ( false !== $shortlink ) { 
  15. return $shortlink; 
  16.  
  17. $post_id = 0; 
  18. if ( 'query' == $context && is_singular() ) { 
  19. $post_id = get_queried_object_id(); 
  20. $post = get_post( $post_id ); 
  21. } elseif ( 'post' == $context ) { 
  22. $post = get_post( $id ); 
  23. if ( ! empty( $post->ID ) ) 
  24. $post_id = $post->ID; 
  25.  
  26. $shortlink = ''; 
  27.  
  28. // Return p= link for all public post types. 
  29. if ( ! empty( $post_id ) ) { 
  30. $post_type = get_post_type_object( $post->post_type ); 
  31.  
  32. if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) { 
  33. $shortlink = home_url( '/' ); 
  34. } elseif ( $post_type->public ) { 
  35. $shortlink = home_url( '?p=' . $post_id ); 
  36.  
  37. /** 
  38. * Filters the shortlink for a post. 
  39. * @since 3.0.0 
  40. * @param string $shortlink Shortlink URL. 
  41. * @param int $id Post ID, or 0 for the current post. 
  42. * @param string $context The context for the link. One of 'post' or 'query',  
  43. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default. 
  44. */ 
  45. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );