Quick Tip: Add Shortlinks to Custom Post Types


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 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 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:

function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {

    // Allow plugins to short-circuit this function.
    $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
    if ( false !== $shortlink )
        return $shortlink;


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'.

 * A function which adds a shortlinks button for 'portfolio' post type
function wptuts_shortlinks_for_portfolio( $shortlink, $id, $context ) {

	// Context can be post/blog/meta ID or query
	$post_id = 0;

	if ( 'query' == $context && is_singular( 'portfolio' ) ) {

		// If context is query use current queried object for ID
		$post_id = get_queried_object_id();

	elseif ( 'post' == $context ) {

		// If context is post use the passed $id
		$post_id = $id;


	// Only do something if of portfolio post type
	if ( 'portfolio' == get_post_type( $post_id ) ) {
		$shortlink = home_url( '?p=' . $post_id );

	return $shortlink;
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.

Related Posts
  • Code
    Quick Tip: Post Types, Taxonomies and PermalinksPost types taxonomies urls
    Custom Post Types and taxonomies are two powerful features of WordPress. Unfortunately, they can have a tendency to cause problems if developers aren't familiar with how permalinks, URLs, and rewriting works within WordPress. In this quick tip, we aim to cover the topic very briefly to make sure you know all you need to know about WordPress URLs, custom post types, taxonomies, and how they all relate.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: A ReviewApplication foundation 400
    Over the past few months, we've been taking a look at all of the features and aspects that make WordPress a potential foundation for application development. In fact, we've spent roughly 15 articles talking about all that WordPress offers. And though we'll be reviewing each of the points in this email, perhaps the biggest thing to take away that building web applications using WordPress is different than using many of the popular frameworks that are currently available namely because WordPress isn't a framework.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: Custom Database QueriesApplication foundation 400
    Throughout this series, we've been looking at the various facilities that make it possible to treat WordPress as a foundation for web application development. Thus far, we've covered a lot of ground: We've talked about how WordPress is more of a foundation rather than a framework. We've discussed the nature of the the Event-Driven Design Pattern. There's been a discussion of Email, User Management, Saving Data, Retrieving Data ...and more. In the most recent articles, we've been talking a look at how to handle queries against the WordPress database through the use of WP_Query and WP_User_Query.Read More…
  • Code
    Creative Coding
    Using WordPress For Web Application Development: Available Features, Part 6: URL Rewriting (or Routes)Application foundation 400
    One of the nicest things about modern web application development frameworks is that they provide a way to generate really clean routes—or URL schemes—that map to the conceptual model of how the application is structured.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: Understanding Events, Actions, and FiltersApplication foundation 400
    Throughout this series, we've been taking a look at how WordPress can be used for building web applications. Up to this point, we've established that WordPress is a foundation - rather than a framework - and we've talked a bit about how we need to avoid trying to shoehorn WordPress into another type of design pattern than that of its native type.Read More…
  • Code
    Creative Coding
    Using WordPress for Web Application Development: The Conceptual ModelApplication foundation 400
    With people beginning to realize WordPress' potential as an application foundation rather than just a content management system or a blogging platform, this series is focusing on just how WordPress can be used for such projects.Read More…