This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
get_shortlink filter
Filters the shortlink for a post.
To use the get_shortlink filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function weplugins_modify_get_shortlink_defaults which takes 4 parameters and we registered using add_filter. The first parameter get_shortlink is the name of the hook, the second parameter weplugins_modify_get_shortlink_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use remove_filter to remove the get_shortlink filter.
Parameters
- $shortlink: (string) Shortlink URL.
- $id: (int) Post ID, or 0 for the current post.
- $context: (string) The context for the link. One of ‘post’ or ‘query’.
- $allow_slugs: (bool) Whether to allow post slugs in the shortlink. Not used by default.
Below are the 4 parameters required to use this hook.
Live Example
Below is an example of how you can use this hook.
apply_filters('get_shortlink', string $shortlink, int $id, string $context, bool $allow_slugs);
Example 1: Modify Shortlink Based on Post ID
In this example, we modify the shortlink based on the post ID.
function weplugins_modify_get_shortlink_defaults($shortlink, $id, $context, $allow_slugs) { if ($id === 42) { $shortlink = 'https://example.com/custom-shortlink'; } return $shortlink; } add_filter('get_shortlink', 'weplugins_modify_get_shortlink_defaults', 10, 4);
Example 2: Add Query Parameter to Shortlink
In this example, we add a query parameter to the shortlink.
function weplugins_add_query_param_to_shortlink($shortlink, $id, $context, $allow_slugs) { if ($context === 'post') { $shortlink .= '?source=post'; } return $shortlink; } add_filter('get_shortlink', 'weplugins_add_query_param_to_shortlink', 10, 4);
Example 3: Conditional Shortlink Modification
In this example, we conditionally modify the shortlink based on the context and ID.
function weplugins_conditional_shortlink_modification($shortlink, $id, $context, $allow_slugs) { if ($context === 'query' && $id === 100) { $shortlink = 'https://example.com/query-shortlink'; } return $shortlink; } add_filter('get_shortlink', 'weplugins_conditional_shortlink_modification', 10, 4);
To remove a hook callback, use the example below.
remove_filter('get_shortlink', 'weplugins_modify_get_shortlink_defaults', 10, 4);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you’re having any trouble using this hook, please contact our team and we’d be happy to assist you.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.