Shortlink trick
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.
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). 🙂
[…] 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. […]
[…] 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 […]
I assume the code needs to be updated to reference wp_get_shortlink()?
Yep. I wrote this when the core code was still in beta.
Hi Otto..
This code works like charm..I’ve used several times but is there any chance that it’ll be developed as a plugin? or is there any similar plugins exists? Anyway.. thanks for sharing the codes.!
Regards..
[…] plugin also hooks into the Shortlink API, meaning that my Twitter plugin will automagically use the new shortlink. You can see that the […]
[…] Source: OttoPress […]
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();' />
You would use CSS to change the background color. I didn’t really cover it, but “.post-shortlink input” would be enough to reference it in my code. You don’t need to add a new class to it.
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 aboveHi Otto n Daniel..
Both of your code for the background style of the shortlink form works for me on my site. Thanks for the hint !
Regards..
Sam
[…] Shortlink-Snippet stammt von Otto on WordPress; die goo.gl Umsetzung von […]
[…] Shortlink-Snippet stammt von Otto on WordPress; die goo.gl Umsetzung von […]
Loving the way the link is highlighted and ready to be copied on click. Thanks for the code.
[…] We can even finish it up in a pretty package by displaying it on the front-end in a selectable text box. […]
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.
hello otto..
This code really worked like a charm.. thnx
I agree with Ozh. There has always been a conflict with shortcodes and stats. by the way, great info. Thumbs up
[…] it does is automatically select the short link once you click into that input box. It’s a neat trick by Otto and definitely makes it easier for your visitors to grab your post’s short link. The […]