Advertisement
  1. Code
  2. WordPress

Quick Tip: Add Shortlinks to Custom Post Types

Scroll to top
Read Time: 2 min

If you use any form of social media, and in particular Twitter, then you have almost certainly come across 'shortlinks' – shortened URLs which act as a label, pointing to a particular page but disguising its lengthier URL. They've been around for over a decade now, but their use really took off with URL shortening services which provided click-through statistics, and character limits on tweets.

WordPress has its own built in 'shortlink' – which, by default, probably isn't very deserving of the name. These are the www.yoursite.com?p=1 links which point to a single post and you can grab them from the 'Get Shortlink' button on your post's edit screen.

There's a good reason for this: WordPress did not want to impose any particular third-party service for URL-shortening, and beneath the default www.yoursite.com?p=1 shortlinks lies an API which allows you to replace it with a more substantially shortened URL from another service – or perhaps even your own.

But WordPress' shortlinks only appear on posts – not pages, or any other post type. In this quick tip I'll be showing you how to rectify this. (And in a similar way you can change the default shortlink entirely by one from a URL shortener service).

Cracking open the source code and locating the wp_get_shortlink() function (see Codex) we find the following:

1
2
function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
3
4
    // Allow plugins to short-circuit this function.

5
    $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
6
    if ( false !== $shortlink )
7
        return $shortlink;
8
9
        ...

The hook pre_get_shortlink, therefore, allows us to by-pass WordPress' default handling of shortlinks. To do that our plug-in needs only to hook onto that filter and return anything other than 'false'.

1
2
/**

3
 * A function which adds a shortlinks button for 'portfolio' post type

4
 */
5
function wptuts_shortlinks_for_portfolio( $shortlink, $id, $context ) {
6
7
	// Context can be post/blog/meta ID or query

8
	$post_id = 0;
9
10
	if ( 'query' == $context && is_singular( 'portfolio' ) ) {
11
12
		// If context is query use current queried object for ID

13
		$post_id = get_queried_object_id();
14
15
	}
16
	elseif ( 'post' == $context ) {
17
18
		// If context is post use the passed $id

19
		$post_id = $id;
20
21
	}
22
23
	// Only do something if of portfolio post type

24
	if ( 'portfolio' == get_post_type( $post_id ) ) {
25
		$shortlink = home_url( '?p=' . $post_id );
26
	}
27
28
	return $shortlink;
29
}
30
add_filter( 'pre_get_shortlink', 'wptuts_shortlinks_for_portfolio', 10, 3 );

Note that if you do not want to change the shortlink (for instance, it's the wrong post type) it's important to return $shortlink (the filtered value that was passed to us by the hook) and not 'false' - since other plug-ins may have already changed $shortlink - and by returning false you would be overriding them.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.