Shortlinks with WordPress 3.0

There will be a new function in the coming 3.0 version, which will create a shortlink to a post. At default it will be in the header of your frontend.

A shortlink would always be with ?p= syntax and the ID of the post.
<link rel='shortlink' href='http://bueltge.de/?p=717' />

To use the shortlink in your theme, you can use a template tag with 4 parameters.

 * @param string $text Optional The link text or HTML to be displayed.  Defaults to 'This is the short link.'
 * @param string $title Optional The tooltip for the link.  Must be sanitized.  Defaults to the sanitized post title.
 * @param string $before Optional HTML to display before the link.
 * @param string $before Optional HTML to display after the link.
 */
the_shortlink($text = '', $title = '', $before = '', $after = '')

Here an example how you can use it in your theme:

if ( function_exists('the_shortlink') ) the_shortlink( __('Shortlink'), __('kurze URL zu diesem Beitrag nutzen'), ' · ' );

The function wp_get_shortlink() is in use in this case. You should use this function if you don’t want to have it displayed (echo).

The link in the head of the frontend is created via a hook and can only get disabled. The integration will be standard in WordPress 3.0:

add_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );

To deactivate the syntax in the head of your template you can put the following code in your functions.php.

remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );

If you like to adjust the shortlink to your liking, you can use several filter hooks.
pre_get_shortlink // Allow plugins to short-circuit this function
get_shortlink // Filter the output


Posted

in

by

Comments

17 responses to “Shortlinks with WordPress 3.0”

  1. Konstantin Avatar

    Yeah, shortlinks are good, though some people like custom domains and alpha-numeric short links 😉 Hurray for 3.0!

  2. Nicolas Avatar

    Is this going to replace the WP short link that you can get already with WP 2.9 or do you have to explicitly add this code to your theme for it to work with your own domain?

  3. Frank Avatar

    @Nicolas: i dont find this functions in WP 2.9, first in WP 3.0 – please see the source.

  4. Tomáš Kapler Avatar

    not much usefull, ID was anyway always visible in URL. I would much more prefer some way to interlink to other posts/pages, which would be connected to ID, but the URL would change to real URL based on my URL settings. So e.g. i want to link article Hello World, id 1 with http://www.example.com/hello-world slug, so that i would not have to write after clicking Anchor button the http://www.example.com/hello-world url and have to repair it when changing URL, but to have some easy way how to select Hello World article from e.g. dropdown, it would add something like [link:1] which would be replaced to http://www.example.com/hello-world or http://www.example.com/?p=1 or whatever i have in my settings.

  5. Jeph Avatar
    Jeph

    I see it in 2.92 and it generates a wp.me link with some alphanumeric combo after the slash.

  6. eavasi™ Avatar

    Short linck is what sorely lacking in the old WordPress. Good luck for us with WordPress 3

  7. dNoxs Avatar

    I’m still waiting wp 3.0 fnal.. thank for tips wp 3.0

  8. Tammy Hart Avatar

    What is the purpose or use for this? This is a great explanation on how to implement it, but i’m just not understanding what it is needed for.

  9. Aizat Faiz Avatar

    @Tammy Hart, this is useful because shortlinks translate to permalinks. Shortened links are useful if you want to give short URLs to a friend, or physical media. Plus shortlinks never change, while permalinks may (by changing a post slug). So if you link things via shortlinks, your site, should theoretically always be functional.

  10. eavasi Avatar

    Its the function which lacked in the older versions!!! (

  11. […] autênticas revoluções no core e na forma como o WordPress trabalha. Recentemente os colegas do WPEngineer escreveram também um artigo interessante sobre os shortlinks no novo WordPress, que são […]

  12. agenerousdesigner Avatar

    Why is this a new feature? ?p=[post’s id here] always works with or without turning on the permalink functions.
    I don’t get it. :S

  13. […] found it interesting to learn that WordPress 3.0 is going to start automatically including something along the lines of this on permalink […]

  14. […] 3.0WordPress 3.0: Ultimate Guide to New FeaturesDisplay Comment Form With Ease In WordPress 3.0Shortlinks with WordPress 3.0Working With MultiSite In WordPress 3.0Personligen så tycker jag att Custom Post Types är den […]

  15. CJEllison Avatar
    CJEllison

    I’ve been searching around to perfect a plugin I need to make for a client. This info put the icing on the cake.

    The “wp_get_shortlink()” tag is exactly what I needed, and I couldn’t find it anywhere online until I found it here.

  16. SK Avatar
    SK

    How can we get the shortlink value?

    This only lets us return it as a formatted link.

    What if I want to show an text box with the URL in it so it is easy to copy?