Using wp_get_shortlink to get the short link for a post in WordPress
The WordPress function wp_get_shortlink
is designed to retrieve the shortlink for a post, page, attachment, or site. This function can be applied to any post type, but it is important to note that it will return an empty string if a shortlink is not available.
The primary use of the wp_get_shortlink
function is to generate a concise URL for sharing. These shorter URLs can be beneficial in contexts where space is limited, such as social media posts or text messages.
It is also worth noting that the wp_get_shortlink
function does not automatically display the shortlink. Instead, it returns the shortlink as a string, allowing developers to decide how and where to use it within their themes or plugins.
In the event that a shortlink is not available for a specific post type, or if the post type is not public, the function will return the URL of the site’s home page.
Parameters of the wp_get_shortlink Function
The wp_get_shortlink
function in WordPress accepts three parameters. These parameters are all optional and are described in detail below:
$id
(integer): This parameter represents a post or site ID. If no value is provided, the default value is 0, which corresponds to the current post or site.$context
(string): This parameter determines whether the ID is a ‘site’ ID, ‘post’ ID, or ‘media’ ID. It also controls whether to consult the post_type of the post (if ‘post’) or the current query (if ‘query’) to determine the ID and context. The default value for this parameter is ‘post’.$allow_slugs
(boolean): This parameter controls whether to allow post slugs in the shortlink. The default value is true. However, it’s up to the plugin to decide how and whether to honor this parameter.
Return Value of the wp_get_shortlink Function
The wp_get_shortlink
function returns a string. This string is a shortlink if one exists for the requested resource and if shortlinks are enabled. If no shortlink exists for the requested resource or if shortlinks are not enabled, the function will return an empty string.
Examples
How to Display the Shortlink of a Post
The following code snippet displays the shortlink of the current post.
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
echo '<p>Shortlink for this post: ' . wp_get_shortlink() . '</p>';
}
}
?>
How to Get the Shortlink of a Specific Post
The following code snippet retrieves the shortlink of a specific post by its ID.
<?php
$post_id = 123; // Replace with your post ID
echo '<p>Shortlink for post with ID ' . $post_id . ': ' . wp_get_shortlink($post_id) . '</p>';
?>
How to Disable Slugs in the Shortlink
The following code snippet retrieves the shortlink of a specific post by its ID, but without allowing slugs in the shortlink.
<?php
$post_id = 123; // Replace with your post ID
echo '<p>Shortlink for post with ID ' . $post_id . ' without slugs: ' . wp_get_shortlink($post_id, 'post', false) . '</p>';
?>
In the above code, the $allow_slugs
parameter of wp_get_shortlink
function is set to false
, which means that post slugs are not allowed in the shortlink.
Conclusion
The wp_get_shortlink
function in WordPress is primarily used to retrieve the shortlink for a post, page, attachment, or blog. This function is especially handy when you need a condensed URL for sharing via social media or other platforms where a shorter link is more desirable. It’s worth noting that the function retrieves the WordPress URL by default, but it can be filtered to return a different shortlink. However, it’s important to remember that not all WordPress installations will have a shortlink provider available, so the function may return the full-length URL in some cases.
Related WordPress Functions
- Getting the URL of a WordPress attachment with wp_get_attachment_url
- Displaying the URL of a post or page in WordPress with the_permalink function
- Getting the admin URL in WordPress using admin_url
- Getting the permalink of a specific page in WordPress using get_page_link
- Getting the permanent link of a post or page in WordPress with get_permalink function
- Getting the current site URL in WordPress using get_site_url
- Getting the current site home URL in WordPress using get_home_url
- Getting the home URL in WordPress with home_url function
- Getting the current site URL in WordPress using site_url