Jetpack Sharing Links Revisited

Jetpack for WordPressA few months ago I posted about how to shorten WordPress Jetpack sharing links. Today, I would like to revisit this topic and provide a cleaner solution that I recently uncovered.

Unlike the previous solution I described, this does not require editing the source of the Jetpack plugin, and thus will persist through plugin updates.

In order to implement this quick fix, you will first need to create a functions.php file in your active theme. If you do not have a custom theme, I would strongly suggest using a child theme, as not doing so will mean losing your modifications if a newer version of your theme is released at a later date.

In short, all that you need to do is hook into the built-in filter provided within the Jetpack source. A filter is a little piece of code that plugin authors (and WordPress core authors) can include to allow users control over some inner functionality. In this case, the filter allows us to tweak what the sharing URL displayed to the end user will look like. If you would like a deeper explanation of content filters and their close cousins, action hooks, the WordPress Codex provides an excellent description of both.

In any event, the fix, shown below, simply returns the shortened URL for use in all the sharing links. Give it a try and be sure to comment if you found this useful!

NOTE: If you would like additional control over which social networks receive shortlinks and which receive the full permalink, you can use the $social_network value, which contains a string with the name of the network.

14 thoughts on “Jetpack Sharing Links Revisited”

    1. I’m glad this helped! This solution should work across the board since it’s simply hooking into existing Jetpack functionality. That said, I have not specifically tested it with custom post types, so if you try it, be sure to post how it went.

      1. Hi Dan, I gave it a try and as always seems to be the case with custom post types (for me and my theme at least) this caused a problem. It works beautifully on normal posts but on custom post types the URL is completely absent – so nothing to share on FB and on Twitter it would just share the post title and via – but no URL. This isn’t your fault – posts and custom post types seem to operate on a different language (possibly a theme issue – maybe someone else with custom post types could check)?

      2. I guess there’s a way to put a conditional in to just run it on a post in the blog – if_post or whatever the right term is. Just thinking out loud. I’m not a coder 🙂

        1. Hmm… I don’t immediately know how this can not be working, other than the fact that it means that either your theme isn’t loading your functions.php file when it loads a page for custom post types OR filters aren’t being executed when your theme loads a custom post type. I think the first is probably more likely, but could be either.

          With these same custom post types, if you remove the filter does the full-length URL appear or is it also empty then?

          If we can narrow down the circumstances under which the problem presents, I may be able to figure out what’s happening…

          1. Hi Dan, thanks. My guess is it may be an issue with Jetpack and custom post types so the hook doesn’t exist? Full URLs work fine with th sharing buttons and functions.php loads fine because the block of sharing buttons towards the top is rendered via a short code I’ve created in it.

  1. Hi Dan, I haven’t tested this but amazing timing. I notice Jetpack was updated 3 days ago and one of the updates is ‘Enhancement: Shortlinks: add Custom Post Type support’ – I wonder whether this may resolve the issue. I’ll let you know if I test it.

    1. That does sound promising! Since we know social links were working, but shortening them was not, I’d bet that’s it. Definitely let me know what you find.

  2. Hi Dan

    Could you explain please how I go about using the $social_network value?

    I want to use the shortlinks for just Twitter but I can’t work out how to do it.

    I sent you an email through your contact form so apologies if you’ve already seem my request.

    1. Hi Tim,

      You should just be able to check whether $social_network is equal to ‘twitter’. If it is, return the shortlink, otherwise just return the $permalink value.

      Untested modified return statement:
      return 'twitter' === $social_network ? wpme_get_shortlink($post_id) : $permalink;


  3. thanks for responding, i tried the new way and it still didn’t work for me, i’m not sure if its something i’m doing wrong…here what i did: i copied the sting of text that you shared with us for the new fix, and pasted it in my function.php at the top after the <?php (not sure if thats where is goes or not), but after saving it and testing the twitter share, there was no share link just the title of the post…can you please let me know what i am doing wrong??

Leave a Reply