For you people who do your own theme work:

<?php if (function_exists('wp_get_shortlink')) { ?>
<div><span class="post-shortlink">Shortlink: 
<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' />
</span></div>
<?php } ?>

Basically that adds a little Shortlink: input box with the shortlink in it. Click the box, and the shortlink becomes selected for easy copy and paste.

It’ll also work with any shortlink plugin that uses the “get_shortlink” filter to override the shortlink properly (like WordPress.com Stats).

Put it somewhere in The Loop or on the Single Post pages or what have you.

Shortlink:

21 Comments

  1. The problem with get_shortlink() is that if you want to define it the way Stats does, you have to make your plugin load before Stats does. I really dislike the whole pluggable way of doing things WP uses.

    Oh and BTW yay for Ottpress, hopefully that will make you write more about WP :)

    • Pluggable functions can be a bit tricky sometimes. However, presumably, any given user would be only activating one shortlink plugin at a time, and thus, no conflict.

      So, my beef there would be that the wp.me shortlink stuff should be in a separate plugin, not in stats. However, they have pretty well tied it to the stats, unfortunately.

      Still, if you want to force your plugin to load before stats does, it’s not hard to do. Get the active_plugins option, manipulate the array to put your plugin ahead of stats, then save the option again. Best to do this on the “activated_plugin” action hook.

      Or just use a plugin name before “S” in the alphabet (that option gets sorted when a plugin is activated). :)

  2. [...] One can filter the_shortlink() function to display custom created shortlinks using pre_get_shortlink() and get_shortlink() hooks. More Information by WPEngineer and OttoPress. [...]

  3. [...] content, using straight not smart quotes, deny comment posting to no referrer requests, and more.Shortlink trick – Otto on WordPress – Many are adding auto-shortlinks or tinyurls to their blog posts for easy tweeting and [...]

  4. I assume the code needs to be updated to reference wp_get_shortlink()?

  5. [...] plugin also hooks into the Shortlink API, meaning that my Twitter plugin will automagically use the new shortlink. You can see that the [...]

  6. Call me stupid, but how would I go about changing the background colour for the “form” .. As in the place where the link is displayed
    Would it be something like <input class='shortlink' type='text' value='' onclick='this.focus(); this.select();' />

  7. I typed
    .post-shortlink input {
    background-color:transparent;
    }

    But nothing happened, in the end I just added style='background-color:transparent;' to the existing code above

  8. [...] Shortlink-Snippet stammt von Otto on WordPress; die goo.gl Umsetzung von [...]

  9. Loving the way the link is highlighted and ready to be copied on click. Thanks for the code.

  10. [...] We can even finish it up in a pretty package by displaying it on the front-end in a selectable text box. [...]

  11. Thanks for the trick but I want when people click on any share button then the shortlink will be shared rather than the permalinks structure. I don’t wants to show Shorten URL.

  12. hello otto..
    This code really worked like a charm.. thnx

  13. I agree with Ozh. There has always been a conflict with shortcodes and stats. by the way, great info. Thumbs up

  14. […] it does is auto­mat­i­cally select the short link once you click into that input box. It’s a neat trick by Otto and def­i­nitely makes it eas­ier for your vis­i­tors to grab your post’s short link. The […]

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Need to post PHP code? Wrap it in [php] and [/php] tags.