Using WordPress ‘pre_get_shortlink’ PHP filter

pre_get_shortlink filter allows you to customize the shortlink generation process for a given post in WordPress.

Usage

add_filter('pre_get_shortlink', 'your_custom_function', 10, 4);
function your_custom_function($return, $id, $context, $allow_slugs) {
    // your custom code here
    return $return;
}

Parameters

  • $return (false|string): Short-circuit return value. Either false or a URL string.
  • $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.

More information

See WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/pre_get_shortlink/

Examples

Custom shortlink with post ID

Customize the shortlink by using the post ID as a path.

function custom_shortlink_with_post_id($return, $id, $context, $allow_slugs) {
    $shortlink = home_url('/') . '?p=' . $id;
    return $shortlink;
}
add_filter('pre_get_shortlink', 'custom_shortlink_with_post_id', 10, 4);

Use the Bitly API to generate shortlinks for your posts.

function bitly_shortlinks($return, $id, $context, $allow_slugs) {
    $post_url = get_permalink($id);
    $bitly_api_key = 'your_bitly_api_key';
    $bitly_url = 'https://api-ssl.bitly.com/v3/shorten?login=your_username&apiKey=' . $bitly_api_key . '&longUrl=' . urlencode($post_url);
    $response = wp_remote_get($bitly_url);
    if (is_wp_error($response)) {
        return $return;
    }
    $json = json_decode(wp_remote_retrieve_body($response));
    if (isset($json->data->url)) {
        return $json->data->url;
    }
    return $return;
}
add_filter('pre_get_shortlink', 'bitly_shortlinks', 10, 4);

Prevent shortlink generation for posts in specific categories.

function disable_shortlinks_for_categories($return, $id, $context, $allow_slugs) {
    $blocked_categories = array('news', 'updates');
    $post_categories = wp_get_post_categories($id, array('fields' => 'slugs'));
    if (array_intersect($blocked_categories, $post_categories)) {
        return false;
    }
    return $return;
}
add_filter('pre_get_shortlink', 'disable_shortlinks_for_categories', 10, 4);

Use a custom domain for your shortlinks.

function custom_shortlink_domain($return, $id, $context, $allow_slugs) {
    $shortlink = 'https://your-custom-domain.com/?p=' . $id;
    return $shortlink;
}
add_filter('pre_get_shortlink', 'custom_shortlink_domain', 10, 4);

Disable shortlinks for custom post types

Prevent shortlink generation for custom post types.

function disable_shortlinks_for_custom_post_types($return, $id, $context, $allow_slugs) {
$blocked_post_types = array('event', 'product');
$post_type = get_post_type($id);
if (in_array($post_type, $blocked_post_types)) {
return false;
}
return $return;
}
add_filter('pre_get_shortlink', 'disable_shortlinks_for_custom_post_types', 10, 4);

Add UTM tracking parameters to your shortlinks.

function utm_parameters_to_shortlinks($return, $id, $context, $allow_slugs) {
    $post_url = get_permalink($id);
    $utm_parameters = '?utm_source=shortlink&utm_medium=post&utm_campaign=blog';
    $shortlink = $post_url . $utm_parameters;
    return $shortlink;
}
add_filter('pre_get_shortlink', 'utm_parameters_to_shortlinks', 10, 4);