wp_get_shortlink( int $id = 0, string $context = 'post', bool $allow_slugs = true ): string
- Since
- 3.0.0
- Source
wp-includes/link-template.php:4144
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
- Scaling
- Constant
- Instructions
- 14–68
- Plugin surface
- 2 hooks
- Called by
- 4
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.
Third-party callbacks on 'pre_get_shortlink', 'get_shortlink' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below wp_get_shortlink() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
$shortlink !== false | 14 | apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && empty($post_id) | 29 | apply_filters(), apply_filters() |
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && empty($post_id) | 32 | apply_filters(), is_singular(), apply_filters() |
$shortlink === false && $context !== "query" && $context === "post" && empty($post_id) | 35–36 | apply_filters(), get_post(), apply_filters() |
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && empty($post_id) | 38–39 | apply_filters(), is_singular(), get_post(), apply_filters() |
$shortlink === false && $context === "query" && is_singular() && empty($post_id) | 38 | apply_filters(), is_singular(), get_queried_object_id(), get_post(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) | 39–41 | apply_filters(), get_post_type_object(), apply_filters() |
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id) | 42–44 | apply_filters(), is_singular(), get_post_type_object(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) | 44–46 | apply_filters(), get_post_type_object(), get_option(), apply_filters() |
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) | 45–48 | apply_filters(), get_post(), get_post_type_object(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) && $post_type | 46 | apply_filters(), get_post_type_object(), home_url(), apply_filters() |
$shortlink === false && $context === "query" && !is_singular() && $context !== "post" && !empty($post_id) | 47–49 | apply_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–51 | apply_filters(), is_singular(), get_post(), get_post_type_object(), apply_filters() |
$shortlink === false && $context === "query" && is_singular() && !empty($post_id) | 48–50 | apply_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_type | 49 | apply_filters(), is_singular(), get_post_type_object(), home_url(), apply_filters() |
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) | 50–53 | apply_filters(), get_post(), get_post_type_object(), get_option(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) && $post_type | 51 | apply_filters(), get_post_type_object(), get_option(), home_url(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) | 51–53 | apply_filters(), get_post_type_object(), get_option(), get_option(), apply_filters() |
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) && $post_type | 52–53 | apply_filters(), get_post(), get_post_type_object(), home_url(), apply_filters() |
$shortlink === false && $context === "query" && !is_singular() && $context === "post" && !empty($post_id) | 53–56 | apply_filters(), is_singular(), get_post(), get_post_type_object(), get_option(), apply_filters() |
$shortlink === false && $context === "query" && is_singular() && !empty($post_id) | 53–55 | apply_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_type | 54 | apply_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–56 | apply_filters(), is_singular(), get_post_type_object(), get_option(), get_option(), apply_filters() |
$shortlink === false && $context !== "query" && $context !== "post" && !empty($post_id) | 55–58 | apply_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_type | 55–56 | apply_filters(), is_singular(), get_post(), get_post_type_object(), home_url(), apply_filters() |
$shortlink === false && $context === "query" && is_singular() && !empty($post_id) && $post_type | 55 | apply_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_type | 57–58 | apply_filters(), get_post(), get_post_type_object(), get_option(), home_url(), apply_filters() |
$shortlink === false && $context !== "query" && $context === "post" && !empty($post_id) | 57–60 | apply_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–61 | apply_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_type | 60–61 | apply_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–63 | apply_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_type | 60 | apply_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–62 | apply_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–65 | apply_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–68 | apply_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–67 | apply_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:
- apply_filters( pre_get_shortlink )filterline 4158 (+14 into the body)
Filters whether to preempt generating a shortlink for the given 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
- the_shortlink()Displays the shortlink for a post.
- wp_admin_bar_shortlink_menu()Provides a shortlink.
- wp_shortlink_header()Sends a Link: rel=shortlink header if a shortlink is defined for the current page.
- wp_shortlink_wp_head()Injects rel=shortlink into the head if a shortlink is defined for the current page.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.