2

New2Tip: Adding A Click Highlighting Shortlink Input To Posts

Learn how to utilize the new WordPress 3.0 shortlinks feature by adding an auto highlighting text input field to your posts that will highlight the current posts shortlink. This makes it easier to Cntrl + C to copy and paste somewhere else for sharing. And it's just kind of cool too....
New2Tip: Adding A Click Highlighting Shortlink Input To Posts

Using WordPress 3.0 Shortlinks

This is a quick tip for how you can add an input form field to your posts that has the posts shortlink now built into WordPress 3.0, that automatically highlights for easy copying, just like the one you see at the bottom right of every post on New2WP.

Here's an example of this

Shortlink:

First check if the function exists incase for some reason it doesn't your theme won't break then.

<?php if ( function_exists( 'wp_get_shortlink' ) ) { ?>

Then create a span element which will include the input field and if you want, the word Shortlink so that people know what it is for.

For the input value echo out the wp_get_shortlink( get_the_ID() ) functions for getting the shortlink of the current post by it's ID.

Then add an onclick event with some Javascript code that says basically, when this is clicked, the input is in focus, this should be selected.

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

Finally, close the if statement first opened to check if the function exists.

<?php } ?>

It's really as simple as that. You can style it with CSS if you want easily, and whatever else you can think of.

Here's The Full Code

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

If you have questions, don't hesitate to post a comment below as always.

Shortlink:

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!

About the Author

Jared is from Boston working as a web and graphic designer. Also owns the design blog Tweeaks.com, and has designed many other websites powered by Wordpress including the New2WP theme.

Level: Noob

User Comments

( ADD YOURS )

  1. Exactly the code I was looking for, ty Jared.

    Used it in conjunction with php-code-widget to use in sidebar


  2. Your welcome :)

Trackbacks


  1. Avatar

    Your Name
    February 4


    CommentLuv badge