wppaste
WordPress

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

Since
3.0.0
Source
wp-includes/link-template.php:4145
Returns a shortlink for a post, page, attachment, or site.

Description

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.

Compatibility

WordPress
since 3.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$idintoptional
A post or site ID. Default is 0, which means the current post or site.Default: 0
$contextstringoptional
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'.Default: 'post'
$allow_slugsbooloptional
Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this. Default true.Default: true

Return value

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

Performance profile

How much work a call to wp_get_shortlink() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–68

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.

Plugin surface
2 hooks

Third-party callbacks on 'pre_get_shortlink', 'get_shortlink' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get()one call below wp_get_shortlink()
  • serializeserialisationmaybe_unserialize()one call below wp_get_shortlink()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 36 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_shortlink() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$shortlink !== false14apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && empty($post_id)29apply_filters(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && empty($post_id)32apply_filters(), is_singular(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && empty($post_id)35–36apply_filters(), get_post(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && empty($post_id)38–39apply_filters(), is_singular(), get_post(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && empty($post_id)38apply_filters(), is_singular(), get_queried_object_id(), get_post(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id)39–41apply_filters(), get_post_type_object(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id)42–44apply_filters(), is_singular(), get_post_type_object(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id)44–46apply_filters(), get_post_type_object(), get_option(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id)45–48apply_filters(), get_post(), get_post_type_object(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) && $post_type46apply_filters(), get_post_type_object(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id)47–49apply_filters(), is_singular(), get_post_type_object(), get_option(), apply_filters()
24 further outcomes, up to 67 instructions
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id)48–51apply_filters(), is_singular(), get_post(), get_post_type_object(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id)48–50apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id) && $post_type49apply_filters(), is_singular(), get_post_type_object(), home_url(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id)50–53apply_filters(), get_post(), get_post_type_object(), get_option(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) && $post_type51apply_filters(), get_post_type_object(), get_option(), home_url(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id)51–53apply_filters(), get_post_type_object(), get_option(), get_option(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) && $post_type52–53apply_filters(), get_post(), get_post_type_object(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id)53–56apply_filters(), is_singular(), get_post(), get_post_type_object(), get_option(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id)53–55apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), get_option(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id) && $post_type54apply_filters(), is_singular(), get_post_type_object(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id)54–56apply_filters(), is_singular(), get_post_type_object(), get_option(), get_option(), apply_filters()
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id)55–58apply_filters(), get_post_type_object(), get_option(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id) && $post_type55–56apply_filters(), is_singular(), get_post(), get_post_type_object(), home_url(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id) && $post_type55apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), home_url(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) && $post_type57–58apply_filters(), get_post(), get_post_type_object(), get_option(), home_url(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id)57–60apply_filters(), get_post(), get_post_type_object(), get_option(), get_option(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id)58–61apply_filters(), is_singular(), get_post_type_object(), get_option(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id) && $post_type60–61apply_filters(), is_singular(), get_post(), get_post_type_object(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id)60–63apply_filters(), is_singular(), get_post(), get_post_type_object(), get_option(), get_option(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id) && $post_type60apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id)60–62apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), get_option(), get_option(), apply_filters()
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id)61–65apply_filters(), get_post(), get_post_type_object(), get_option(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id)64–68apply_filters(), is_singular(), get_post(), get_post_type_object(), get_option(), get_option(), home_url(), apply_filters()
$shortlink === false && $context === "query" && is_singular() && !empty($post_id)64–67apply_filters(), is_singular(), get_queried_object_id(), get_post(), get_post_type_object(), get_option(), get_option(), home_url(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 82 instructions, 14–68 executed per call, 11 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_get_shortlink() runs, in this order:

  1. apply_filters( pre_get_shortlink )filterline 4159 (+14 into the body)

    Filters whether to preempt generating a shortlink for the given post.

  2. apply_filters( get_shortlink )filterline 4201 (+56 into the body)

    Filters the shortlink for a post.

Uses · 7

  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
  • get_queried_object_id()Retrieves the ID of the currently queried object.
  • get_post()Retrieves post data given a post ID or post object.
  • get_post_type_object()Retrieves a post type object by name.
  • get_option()Retrieves an option value based on an option name.
  • home_url()Retrieves the URL for the current site where the front end is accessible.

Used by · 4

Source code

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 );}

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.